Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: regexp - replace spaces in quoted string

by planetscape (Chancellor)
on Sep 06, 2005 at 10:43 UTC ( [id://489424]=note: print w/replies, xml ) Need Help??


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

YAPE::Regex::Explain will help you:

The regular expression: (?-imsx:s/(".*?")/($x=$1)=~y: :_:, $x/ge) 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): ---------------------------------------------------------------------- s/ 's/' ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- " '"' ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- / '/' ---------------------------------------------------------------------- ( group and capture to \2: ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- x= 'x=' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- 1 '1' ---------------------------------------------------------------------- ) end of \2 ---------------------------------------------------------------------- =~y: :_:, '=~y: :_:, ' ---------------------------------------------------------------------- $ before an optional \n, and the end of the string ---------------------------------------------------------------------- x/ge 'x/ge' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------

HTH,


Update: See below for corrections. (Thanks, japhy!)

planetscape

Replies are listed 'Best First'.
Re^3: regexp - replace spaces in quoted string
by uksza (Canon) on Sep 06, 2005 at 11:11 UTC
    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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found