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

Re^2: You won't believe what this regular expression does!

by haukex (Archbishop)
on Feb 25, 2021 at 14:42 UTC ( [id://11128781]=note: print w/replies, xml ) Need Help??


in reply to Re: You won't believe what this regular expression does!
in thread You won't believe what this regular expression does!

Are there work-arounds to achieve what you want?

I sometimes use (?:\n|\z) to be explicit that I want the line endings to be consumed by the engine.

Replies are listed 'Best First'.
Re^3: You won't believe what this regular expression does!
by LanX (Saint) on Feb 25, 2021 at 15:15 UTC
    Thanks! :)

    But please note the second fOO

    DB<55> p "hello\nfoo" =~ s/o*(?:\n|\z)/O/gmr; hellOfOO DB<56>

    I'm busy right now, but I seem to remember that one could use features for atomic matches...

    I'll try later...°

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

    update

    °) nah doesn't help, since it's not a backtracking problem.

      But please note the second fOO

      Yes, good point! I think the main question is what the intent of the regex is. If it's "replace any o's at the end of each line", then the better solution is, as you said, /o+$/, and using o* is the "mistake".

        No, the intent is to ensure that the string ends in just one o, no less, no more than one.

        In real life, those lines were paths, and I wanted all of then ending in a single slash. For instance:

        /foo --> /foo/ /foo/ --> /foo/ /foo////// --> /foo/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found