Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex boundary match (updated demo code)

by LanX (Saint)
on Feb 08, 2020 at 14:00 UTC ( [id://11112623]=note: print w/replies, xml ) Need Help??


in reply to Regex boundary match

Did you try a negative lookahead assertion to exclude the dash ?

update

DB<21> $test = qq|Some test. This is a test-with a dash. We want to m +atch test here| DB<22> x $test =~ /\b(test)(?!-)\b/g 0 'test' 1 'test' DB<23> print pos($test),":$1\n" while $test =~ /\b(test)(?!-)\b/g 9:test 60:test

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Regex boundary match (updated demo code)
by ultranerds (Hermit) on Feb 08, 2020 at 14:08 UTC
    Thanks. I ended up with ([^\-]). Is your version quicker? Or negligible difference? (its going to be run quite a lot of times)
      I ended up with ([^\-]). ... difference?

      It depends on exactly what you want.  ([^\-]) requires a character match and consumes a character. As LanX pointed out,  ([^\-]|$) consumes a character if it can. Neither seem to do what you want:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'test atest atested tested test- -test test .test test. test' +; ;; $s =~ s/\b(test)([^-])/DONE/g; print qq{'$s'}; " 'DONEatest atested DONEd test- -DONEDONE.DONEDONE test' c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'test atest atested tested test- -test test .test test. test' +; ;; $s =~ s/\b(test)([^-]|$)/DONE/g; print qq{'$s'}; " 'DONEatest atested DONEd test- -DONEDONE.DONEDONE DONE'
      Maybe this does:
      c:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'test atest atested tested test- -test test .test test. test' +; ;; $s =~ s/\b(test)(?!-)\b/DONE/g; print qq{'$s'}; " 'DONE atest atested tested test- -DONE DONE .DONE DONE. DONE'

      Update: Just noticed from the timestamps that ultranerds already decided to go with LanX's suggestion. :)


      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://11112623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-18 08:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found