Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: regexp - replace spaces in quoted string

by uksza (Monsignor)
on Sep 06, 2005 at 11:11 UTC ( #489436=note: print w/ replies, xml ) Need Help??


in reply to Re^2: regexp - replace spaces in quoted string
in thread regexp - replace spaces in quoted string

s/(".*?")/($x=$1)=~y: :_:, $x/ge) [CUT] =~y: :_:, '=~y: :_:, ' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- x/ge 'x/ge'
Hm... Is it good explanation??
I'll try in my poor english:
s/(".*?")/($x=$1)=~y: :_:, $x/ge) ^^^^^^^^^1 ^^^^^^^2 ^^^^^^^3 ^^4 ^^^5
1) take ALL between " and "
2) So you have ALL in $1. $1 is read only, so copy $1 to $x. And now, for one moment forget about all and work only on $x (this is thanx /e which "wraps an eval{...} around the replacement string and the evaluated result is substituted for the matched substring".
3)in $x replace all ' ' to '_'
4)put replaced $x into first s/from/to/
5)g - works globaly; e - eval the replacment string

greets
Uksza


Comment on Re^3: regexp - replace spaces in quoted string
Select or Download Code
Re^4: regexp - replace spaces in quoted string
by planetscape (Canon) on Sep 06, 2005 at 12:11 UTC

    While the explanation provided by YAPE::Regex::Explain will probably be inferior to that which a human well-versed in regexps could produce, the advantage in using the module is that an explanation can be produced without having access to the well-versed human. It is available to anyone with access to CPAN, and therefore can be quite useful to the learner. :-)

    planetscape
      Um, you mis-used my module. Only the LEFT-HAND side of a s/// is a regex; therefore, only the left-hand side should be sent through YAPE::R::E.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        Sounds like a feature request to me. ;)

        Another suggestion: if the whole regex is surrounded by a fully qualified (?xi-sm: ... ) block, then it's likely created by the qr// stringification, and not as presented. I would consider stripping that off and then explain the modes at the end along with any /g /e explanations.

        --
        [ e d @ h a l l e y . c c ]

        Oopsie. I'm on drugs - that's my excuse.

        (No, really - oral surgery last Wednesday. That's my story, and I'm sticking to it!)

        Corrected stuff:

        The regular expression: (?-imsx:(".*?")) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

        The point about YAPE::Regex::Explain being useful to someone learning regexes is still perfectly valid, despite my mistake. :-)

        planetscape

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2015-07-06 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls