Let's rewrite your regular expression with the (?x) flag and comment it shall we? That gives us
qr{(?smx) \.? # Match the first instance of 0 or 1 one periods ( # then capture \S+ # one or more non whitespace characters ) }
So, if we follow that recipe on the string human.NT_113898, the matcher looks at the start of the string, sees that the test for 0 or 1 periods succeeds there, so it scarfs up the rest of the string into $1. Which isn't what you wanted. What you actually want depends a little on what you're expecting as input. Assuming that there's always going to be at least one period in the input string, something like
qr{(?smx) \. # Find a full stop (.*) # and capture everything after it }
will do. However, f there's the possibility of there not being a period, you might have to do
qr{(?smx) ^ # From the beginning (?: # In a group... [^.]* # Match any character except period, any number of times \. # followed by a period )? # math the group 0 or 1 times (.*) # then capture everything else }
If there's a trick to understanding why a regular expression doesn't do what you want, it's to break it down like this and go through each subexpression and explain to yourself what it's trying to match. Most of the time this narrative approach will lead you to your bug and to its fix remarkably quickly.

In reply to Re: Removing first part of string with regex by pdcawley
in thread Removing first part of string with regex by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.