Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Regex variables with delimiters

by AnomalousMonk (Archbishop)
on May 18, 2019 at 01:26 UTC ( [id://11100214]=note: print w/replies, xml ) Need Help??


in reply to Regex variables with delimiters

... the =~ operator has been cunningly designed so that ... there is a builtin assumption that it uses '/' delimiters ...

A counterexample:

c:\@Work\Perl\monks>perl -wMstrict -le "my $raw = '/this/stuff/'; print 'raw: yes' if 'does/this/stuff/match?' =~ $raw; ;; my $regex = qr/$raw/; print 'regex: yes' if 'and/this/stuff/also?' =~ $regex; " raw: yes regex: yes


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Regex variables with delimiters
by clerew (Novice) on May 19, 2019 at 15:11 UTC

    Yes that works, but my copy pf the O'Reilly Camel book seems to suggest otherwise :-(.

    Anway, after a Good Night's sleep I worked out how I should have done it:

    $regexp = <STDIN>

    probably with a chomp somewhere, and then

    $line =~ m?$regexp?

    That seems to work fine, so thanks to all who replied.

    Additional remark on May 20th

    actually, that didn't work. Due to the special peculiarities of m?xxxxx? matches, you need to call reset whenever you change $regexp. Much simpler just to use a different delimiter, so here is my final versions, now working as intended:

    $line =~ m#$regexp#
      ...  $line =~ m?$regexp? ... That seems to work fine ...

      Again,  m?...? is a special case of the  m// operator (per this), but if you're happy with its one-match-only behavior, I'm happy too! :)


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 07:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found