Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Help combining two regex's into one

by Anonymous Monk
on Jan 15, 2013 at 02:38 UTC ( #1013303=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Seeking wisdom of the Perlwiser monks ...

Created this in pieces, combined and whittled the pieces down to the following two lines and cannot figure out how to combine them to the desired one liner.

my ($str) = $input =~ m#^-L(((?!\s*-).)*)#; $input =~ s#-lmylib#$str/mylib.a#;

Any suggestions ??

The details:

# # Input : -L/usr/local/lib -lmylib -lz # Desired output: -L/usr/local/lib /usr/local/lib/mylib.a -lz # use strict; use warnings; # The input string my $input = '-L/usr/local/lib -lmylib -lz'; # The meat my ($str) = $input =~ m#^-L(((?!\s*-).)*)#; $input =~ s#-lmylib#$str/mylib.a#;

Thank y'all for doing what ya do!

Replies are listed 'Best First'.
Re: Help combining two regex's into one
by Kenosis (Priest) on Jan 15, 2013 at 03:19 UTC

    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;


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

      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


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


        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.


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

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

Re: Help combining two regex's into one
by Anonymous Monk on Jan 15, 2013 at 08:45 UTC
    What are you working on? Normally you'd leave that up to the compiler/linker
      et al ...

      No, I'm not trying to process a command line.

      I am coercing an automatically generated Makefile into submission as it's being written.

      As in overriding MakeMaker's limited and IMNSHO failed sanity checking.
      MakeMaker will not allow (at least that I've been able to discover) a fully qualified library name.

      The one I hacked together from Kenosis solution gets it done, and it looks just like me ... UGLY!

      Now, I've learned to accept being ugly, it's an unchangeable fact of life.

      Fortunately for code, it can be changed, it can be made to look pretty if one has the skills!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013303]
Approved by Kenosis
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2018-05-23 13:59 GMT
Find Nodes?
    Voting Booth?