Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by salva (Canon)
on Feb 26, 2021 at 11:55 UTC ( [id://11128826]=note: print w/replies, xml ) Need Help??


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

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/

Replies are listed 'Best First'.
Re^6: You won't believe what this regular expression does!
by haukex (Archbishop) on Feb 26, 2021 at 13:16 UTC
    In real life, those lines were paths, and I wanted all of then ending in a single slash.

    Ah, I see. You want canonpath from File::Spec ;-) (doesn't add the trailing slash, but there are multiple possible solutions for that as well)

    And this is admittedly not the prettiest solution, but it works: s{(?| /* (\n) | /+ (\z) | [^/] \K (\z) )}{/$1}mgrx. Or just split the input string on \n...

Re^6: You won't believe what this regular expression does!
by LanX (Saint) on Feb 26, 2021 at 13:17 UTC
    > No, the intent is to ensure that the string ends in just one o, no less, no more than one.

    And how did you solve it in the end?

    ...

    You won't believe how I did this!

    DB<39> $_="/foo\n/foo/\n/foo/////" DB<40> say "$_ --> ",s(/*$)(/)r for split /\n/ /foo --> /foo/ /foo/ --> /foo/ /foo///// --> /foo/

    ;-P

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

      "hello\nhell\nhelloooo\nhello" =~ s/o+$//gmr =~ s/$/o/gmr

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 10:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found