Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Help combining two regex's into one

by Kenosis (Priest)
on Jan 15, 2013 at 03:19 UTC ( #1013304=note: print w/ replies, xml ) Need Help??


in reply to Help combining two regex's into one

Perhaps the following will be helpful:

use strict; use warnings; my $input = '-L/usr/local/lib -lmylib -lz'; $input =~ s!^-L(.+)\s+\K-l(.+)(?=\s+.+)!$1/$2.a!; print $input;

Output:

-L/usr/local/lib /usr/local/lib/mylib.a -lz

The \K says to keep all to its left. Here's an explanation of the regex.


Comment on Re: Help combining two regex's into one
Select or Download Code
Re^2: Help combining two regex's into one
by Anonymous Monk on Jan 15, 2013 at 06:49 UTC
    Awesome, I got exactly what I asked for ... plus!

    Unfortunately what I asked for wasn't exactly right!

    I should have included that there could be other and or more options following '-lmylib' ...
    The known is it'll always be the first two elements in this order: '-L<path> -lmylib'
    Of course the path is a variable, Number of options following 'mylib' is variable.

    Your solution is slick, I like the capture and reuse of 'mylib', make maintaining the code easier.

    The \K is the clue ... However, it's going to take some time for the whole of it to sink into this pea brain of mine.

    A great link to Regular Expressions 101 expression tester! Hadn't seen/tried that one.

    I made a mess out of your work to get it to work, the result

    s!^-L(.+)\s+\K-lmylib!$1/mylib.a!

    I prefer your cleaner solution except additional options make it fail.

    Thanks

      I can't quite tell if you're processing command line arguments or not. So if not, ignore the rest of this post.

      Processing command-line arguments can get tricky, so I won't talk you out of doing it yourself. However, I *do* want to point out that it's complex enough that various people have created various packages to handle them. You may find it worth your while to use one of them, rather than wrestling with subtle errors as you add yet more options. I usually use Getopt::Long, but you should review several before choosing. Getopt::Long is pretty flexible, but requires a bit more effort than some others, so you might not like it as much as I do.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Re^2: Help combining two regex's into one
by AnomalousMonk (Abbot) on Jan 16, 2013 at 01:07 UTC
    The \K says to keep all to its left.

    The  \K regex operator acutally functions to ignore everything to its left WRT the final match and only include or 'keep' text to its right. See discussion of  "(?<=pattern)" "\K" in the Look-Around Assertions section of perlre.

      From perlre:

      • "...\K , which causes the regex engine to "keep" everything it had matched prior to the \K..."
      • "\K [6] Keep the stuff left of the \K..."

      I suspect the K was not an accidental mnemonic device for its \Keeping operation...

        Just a matter of perspective: the left part which is kept unchanged is the one ignored from substitution, because the right part is kept in '$&'.

        So both parts are kept, just in different aspects.

        Almost as confusing as using single line and multi line modifiers at the same time...

        From perlre, identical in both Strawberry 5.14.2.1 local docs and 5.16.2 on-line docs (emphases added):

        • \K     [6]   Keep the stuff left of the \K, don't include it in $&
          (Note [6] simply refers to the discussion in the "Look-Around Assertions" section.)
        • From "Look-Around Assertions":
          There is a special form of this construct, called "\K", which causes the regex engine to "keep" everything it had matched prior to the "\K" and not include it in $&.

        Hmmm...   I suppose when I read these sections heretofore, my attention was drawn to the "don't include..." clauses and I more or less ignored the "left of" and "prior to" statements. My naive association of "keep" is with inclusion: in this specific instance, everything to the right of  \K is (or could be) included in $&. The docs seem to be using "keep" in the sense of "keep back", i.e., "to exclude": everything to the left of  \K is "kept back" from inclusion in $&. Perhaps they are discussing the behavior  \K in the context of substitution: everything left of the  \K is not included in $& and so cannot be zapped by substitution: it is "kept safe" from substitution. But  \K is equally valid and useful in both  m// matching and  s/// substitution, so...

        I can't say I would agree with the perhaps subtle interpretation given to "keep" in the documentation, but it is fully clarified by the "don't include" language and by the examples, so I doubt this qualifies as a doc-bug.

        Update: LanX: Almost as confusing like using single line and multi line modifier at the same time... ++That!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2014-09-15 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls