What exactly is the role of the ^

In a character class (the square brackets thing), if you put ^ (called a "caret") first before any characters, it negates the whole class, so it means "any character _except_ one of these". (You can include a caret in a character class and have it just be a literal ^ character, if it's not the first thing; it only has this special negate-the-class meaning if it's first.)

This is all only in a character class, though. In other regular-expression contexts, the caret is a zero-width positive assertion that matches only at the beginning. (Whether it matches at the beginning of the line or the beginning of the string depends on the options you're using.)

So this regular expression matches any string that starts with a vowel:

/^[aeiou]/i

But this one matches any string that starts with something *other* than a vowel:

/^[^aeiou]/i

(Technically, I should say "unaccented Latin-alphabet vowel" in both of those statements, but if you assume strings are composed of printed ASCII characters then what I said is good enough. The extra verbiage would only be necessary to accommodate other character sets, notably Unicode. And anyway, the meaning of the word "vowel" is really not the main point here.)

-- 
We're working on a six-year set of freely redistributable Vacation Bible School materials.

In reply to Re: How do I reverse the order of the first and last word of a string? by jonadab
in thread How do I reverse the order of the first and last word of a string? by ferrispike12

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.