Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

regex: finding something that doesn't contain a quotation?

by rmexico (Novice)
on Feb 11, 2006 at 03:55 UTC ( #529519=perlquestion: print w/ replies, xml ) Need Help??
rmexico has asked for the wisdom of the Perl Monks concerning the following question:

i'm looking to find something like the following:
blah.boo(getSomething()) ;
but not the following:
blah.boo("bingo");
so basically, find an open paren, then whatever's inside the open paren until a final close paren and a semi-colon (the close paren and the semi- need not be butted against one another). how do i match what's in the parens only if it doesn't have a quotation mark?
$filecontents =~ /.*?$blah\.([0-9a-zA-Z_\-]+)\((.*?)\).*\;/
so $2 up there, how do i say, gimme anything only if it doesn't have a quotation mark?

Comment on regex: finding something that doesn't contain a quotation?
Select or Download Code
Replies are listed 'Best First'.
Re: regex: finding something that doesn't contain a quotation?
by GrandFather (Sage) on Feb 11, 2006 at 04:59 UTC

    Two tests:

    use strict; use warnings; use Regexp::Common qw/balanced/; while (<DATA>) { chomp; next if ! /($RE{balanced}{-parens=>'()'})/ or $1 =~ /^\("/; print "$_\n"; } __DATA__ blah.boo(getSomething()) blah.boo("getSomething") blah.boo(getSomething("something else")) blah.boo("getSomething()")

    Prints:

    blah.boo(getSomething()) blah.boo(getSomething("something else"))

    DWIM is Perl's answer to Gödel
Re: regex: finding something that doesn't contain a quotation?
by prasadbabu (Prior) on Feb 11, 2006 at 04:12 UTC

    Your question is somewhat not very clear, but if i understood correctly, this works fine.

    use strict; use warnings; use Regexp::Common qw/balanced/; #my $string = 'blah.boo(getSomething())'; my $string = 'blah.boo("getSomething")'; my ($match) = $string =~ /blah.boo($RE{balanced}{-parens=>'()'})[^;]*; +/; print $string, "\n" if ($match !~ /"/);

    Prasad

Re: regex: finding something that doesn't contain a quotation?
by l.frankline (Hermit) on Feb 11, 2006 at 04:24 UTC

    Hi,

    Can you mention, what is initialized in $blah variable, so that I can help you more efficiently, anyany...

    A small change is missing in your regex. Get the solution below:

    print $& if ($filecontents =~ /blah\.boo\(\w+\(\)\)\;/);

    Cheers,
    Franklin

    Don't put off till tomorrow, what you can do today.

Re: regex: finding something that doesn't contain a quotation?
by injunjoel (Priest) on Feb 11, 2006 at 05:07 UTC
    Greetings,
    This is just a quick suggestion
    based on my understanding of your question...
    $filecontents =~ /.*?$blah\.([\w-]+)\(([^"]*)\)\s*;/; #not really sure what $blah is holding but #the [\w-] can replace your [0-9a-zA-Z_\-] character class #and \(([^"]*)\) is a greedy way of saying #anything inside of parens that is not a quote.
    -InjunJoel

    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2015-07-30 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls