Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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?

Replies are listed 'Best First'.
Re^2: Space or end of string in regex
by davies (Parson) 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?
[choroba]: three
[choroba]: I mean I haven't used DBIx::Class nor M:S, only Moo
[1nickt]: Haha, I was learning about Linux filesystem trees trying to understand how Moo fits.
[1nickt]: I just got my feet wet with some DBIC coding this last week. I feel "meh" about it so far. It was pretty easy to add a sub to the DBIC class to filter search results ...
[1nickt]: ... in the calling code ... $rs = $rs->maturity(" stable");. But I don;t enjoy feeling that far away from the SQL and the DB, really.
[1nickt]: The same project (CPAN Testers API) is using experimental signatures (and therefore 5.024) ... which is what replaced Method::Signatures , afaict.
[1nickt]: But the new $job is on an older Perl and therefore using M::S, the syntax of which looks clunky to me, so far.

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-11-24 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (349 votes). Check out past polls.

    Notices?