http://www.perlmonks.org?node_id=951550


in reply to Re: Space or end of string in regex
in thread Space or end of string in regex

This works perfectly, thank you. I'm not worried about multiple spaces as a single space is enough to indicate that this is a keyword that needs special treatment and multiple spaces are handled in that routine. My problem was not realising that I needed to anchor at the start. I was therefore looking for a character class that contained space or end of string and therefore, once again, flailing at demons.

Am I right in thinking that replacing the slashes with square brackets is optional?

Regards,

John Davies

Replies are listed 'Best First'.
Re^3: Space or end of string in regex
by BrowserUk (Patriarch) on Feb 02, 2012 at 23:15 UTC
    Am I right in thinking that replacing the slashes with square brackets is optional?

    Indeed. Purely personal preference.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re^3: Space or end of string in regex
by Marshall (Canon) on Feb 03, 2012 at 20:47 UTC
    if( $line =~ m[^REM $sTest\s*$]i ) {
    If you use the //, then you don't need the "m" in front, but otherwise its just style preference.

    There is one other fine point about this, if $sTest could contain some characters that would normally mean something to the regex engine, like a "(" or whatever, you can specify to interpret the string $sTest literally - meaning ignore the meaning of those characters. This is done by surrounding $sTest with a \Q \E pair.

    if( $line =~ /^REM \Q$sTest\E\s*$/i ) {