Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re^3: regexp - replace spaces in quoted string

by uksza (Abbot)
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


Replies are listed 'Best First'.
Re^4: regexp - replace spaces in quoted string
by planetscape (Chancellor) 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. :-)

      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

        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. :-)

        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 ]

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://489436]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2017-06-23 03:38 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (533 votes). Check out past polls.