Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

regex needed

by doubledecker (Scribe)
on Jul 12, 2012 at 10:28 UTC ( #981354=perlquestion: print w/replies, xml ) Need Help??
doubledecker has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I'm trying to match the regex but doesn't seem it is working for few set of examples. I need to capture anything after $$ in $1 and $2.
use strict; my $line = '%TMP%\$$(DATE)_RE_IRD_Soc'; my $line2 = '%RESULTS%\$$(DATE_USA)' my $line3 = '%RESULTS%\$$(1110_DATE_USA)'; my $regex = qr/\((.+)\)(.+)*/gi;

Replies are listed 'Best First'.
Re: regex needed
by moritz (Cardinal) on Jul 12, 2012 at 11:50 UTC
Re: regex needed
by Anonymous Monk on Jul 12, 2012 at 10:47 UTC

    Your code doesn't show any attempts at matching , or you want what?

    my( @this ) = $line =~ /$regex/gi; my @want = ( 'DATE', '_RE_IRD_Soc' ); Test::More::is_deeply( \@this, \@want, "got what I needed");

    This ought to do

    $regex = qr{ \$ \( ( [^\)]+ ) # $1 \) (.*) # $2 }xmi;

    Death to Dot Star!

Re: regex needed
by cheekuperl (Monk) on Jul 12, 2012 at 10:44 UTC
    I need to capture anything after $$
    my $line = '%TMP%\$$(DATE)_RE_IRD_Soc'; my $line2 = '%RESULTS%\$$(DATE_USA)' ; my $line3 = '%RESULTS%\$$(1110_DATE_USA)'; @lines=($line,$line2,$line3); foreach $elem (@lines) { while($elem=~m/(.*)\$\$(.*)/g) { print "\nBefore: [$1], After : [$2]"; } }
    I need to capture anything after $$ ,in $1 and $2.
    You have not mentioned what exactly you want in $1 and $2.
      sorry, from $line, I need DATE in $1 and _RE_IRD_Soc in $2 $line2, $line3 doesn't have anything in $2. my idea is to have one regex which satisfy both conditions.

        It may help to talk it through in words first. The pattern you're looking at is:

        • Two dollar signs (metacharacters, will need escaping)
        • An open parenthesis (also needs escaping)
        • Some text up to a closed parenthesis (capture this in $1)
        • A closed parenthesis (escaped)
        • Anything else that exists beyond that (captured in $2)

        Now just put those elements together in a regex:

        if( $line =~ m[ \$\$ # two dollar signs \( # open paren ([^)]+) # capture until closed paren \) # close paren (.*) # capture the rest, if any ]x ){ # do stuff with $1 and $2 }

        Aaron B.
        Available for small or large Perl jobs; see my home node.

        Check if this works:
        my $line = '%TMP%\$$(DATE)_RE_IRD_Soc'; my $line2 = '%RESULTS%\$$(DATE_USA)' ; my $line3 = '%RESULTS%\$$(1110_DATE_USA)'; @lines=($line,$line2,$line3); foreach $elem (@lines) { if($elem=~m/.*\$\$(\(.*\))(.*)?/) { print "\nFirst: $1, Second: $2"; } }
Re: regex needed
by BillKSmith (Vicar) on Jul 12, 2012 at 11:52 UTC
    The /g option on your regular expression does not do anything useful here and can cause problems.
      Yup, its a syntax error
Re: regex needed
by monsoon (Pilgrim) on Jul 12, 2012 at 15:01 UTC
    Another way of doing it
    my (undef,$one,$two) = split /[()]/, $line;
Re: regex needed
by ckj (Chaplain) on Jul 12, 2012 at 19:02 UTC
    This will work:
    my $line = '%TMP%\$$(DATE)_RE_IRD_Soc'; my $line2 = '%RESULTS%\$$(DATE_USA)' ; my $line3 = '%RESULTS%\$$(1110_DATE_USA)'; foreach my $x ($line, $line2, $line3) {print "lklklk"; while($x=~m/\$\$\((.*?)\)(\w*)/g) { print "1st Element is [$1] and second is : [$2]\n"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://981354]
Approved by Corion
Discipulus thanks back and passes around a weis bier in the berghastof hummelei celebrative glass

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2018-05-25 17:01 GMT
Find Nodes?
    Voting Booth?