Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Both @slavy13 and @dreftymac give you the basic solution with pointers, but...</p> <ul> <li>You can use <code>\d</code> inside a character class to mean a digit.</li> <li>You don't need to put blank into a character class to match it (except, perhaps, for clarity, though that is debatable).</li> <li>You can use <code>[:alpha:]</code> inside a character class to mean an alpha character, <code>[:digit:]</code> to mean a digit, and <code>[:alnum:]</code> to mean an alphanumeric (specifically not including underscore, unlike <code>\w</code>). Note that these character classes might mean more characters than you expect; think of accented characters and non-arabic digits, especially in Unicode.</li> <li>If you want to capture the whole of the information after the space, you need the repetition inside the capturing parentheses.</li> </ul> <p>Contrast the behaviour of these two one-liners:</p> <pre><code>perl -n -e 'print "$2\n" if m/\bI( {1,2})([a-zA-Z\d\\]){2,13}\b/' perl -n -e 'print "$2\n" if m/\bI( {1,2})([a-zA-Z\d\\]{2,13})\b/' </code></pre> <p>Given the input line "<code>I a123</code>", the first prints "3" and the second prints "a123". Obviously, if all you wanted was the last character of the second part of the string, then the original expression is fine. However, that is unlikely to be the requirement. (Obviously, if you're only interested in the whole lot, then using '<code>$&amp;</code>' gives you the matched text, but it has negative efficiency implications.)</p> <p>I'd probably use this regex as it seems clearest to me:</p> <pre><code>m/\bI( {1,2})([[:alnum:]\\]{2,13})\b/ </code></pre> <p>Time for the obligatory plug: read Jeff Friedl's "<a href="http://oreilly.com/catalog/9780596002893/index.html" rel="nofollow noreferrer">Mastering Regular Expressions</a>".</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload