Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Space or end of string in regex

by BrowserUk (Pope)
on Feb 02, 2012 at 22:44 UTC ( #951548=note: print w/ replies, xml ) Need Help??


in reply to Space or end of string in regex

You are almost there:

if( $line =~ m[^REM $sTest ?$]i ) { # ... }

The $ means end-of-line. The ? following the space preceding the $, means the space is optional.

More formally, zero or one spaces may exist here.

That said, allowing for just one trailing space is unusually mean. More normal would be to allow only whitespace to follow the required text before the end-of-line. Which could be written:

if( $line =~ m[^REM $sTest\s*$]i ) { # ... }

Formally zero or more whitespace characters may precede the end-of-line.


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?


Comment on Re: Space or end of string in regex
Select or Download Code
Re^2: Space or end of string in regex
by davies (Vicar) on Feb 02, 2012 at 23:00 UTC

    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

      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?

      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 ) {

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://951548]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-08-21 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls