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

Re^2: How to substitute something from only between two specified charecters (sub_question)

by ww (Bishop)
on Jun 29, 2011 at 13:45 UTC ( #911972=note: print w/ replies, xml ) Need Help??


in reply to Re: How to substitute something from only between two specified charecters
in thread How to substitute something from only between two specified charecters

OP can probably extrapolate, and maybe that's why the parent stops just short of actually answering the original question: how to remove spaces, but only in the location field -- or learn from some other replies.

But just in case the assumption above is wrong, assign $2 to a named var ($second maybe) and remove spaces:

$second =~ s/\s*//g; ... say "C/D/E/F - $second"; ...

BUT that's not really the point of this post; rather (perhaps because /me is suffering brain-freeze, why the heck is the second capture ((.*\S)) a-greedy-anything followed by anything-not-whitespace working?

Y::R::E isn't helping this morning; neither is a recheck of (some obvious parts of) Mastering Regular Expressions

And in case my brain-freeze isn't clear, that chill is telling me that s+(.*\S)\s+(\S+) should capture the location-field and everything else up to the last space, before "HA". That's obviously wrong, but why?

Can someone, please, provide a the meat for a slap my forehead, grunt "Duh!" moment?


Comment on Re^2: How to substitute something from only between two specified charecters (sub_question)
Select or Download Code
Re^3: How to substitute something from only between two specified charecters (sub_question)
by wind (Priest) on Jun 29, 2011 at 19:37 UTC

    I wrote the regex that way to have an explicit boundary between the second field and the spacing separating it from the third field. I didn't want to eat any extra spacing.

    I could have accomplished this in one of three ways:

    1. 1) Explicitly specify that the field shouldn't contain a space at the end like I did. (.*\S)\s+
    2. 2) Use an explicit boundary like (.*)\b\s+
    3. 3) Or rely on non-greedy matching: (.*?)\s+ Which would work because of the hard boundaries for the other fields

    In the end, the third method above would probably appear the cleanest, but they all accomplish the same thing in the context of the rest of the regex.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-29 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (226 votes), past polls