Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

A Question on Regex

by Hari7 (Initiate)
on Sep 07, 2011 at 07:21 UTC ( [id://924550]=perlquestion: print w/replies, xml ) Need Help??

Hari7 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Everyone,

I am a Newbie and would like to know how I could do the following regex( really tried for half a day before posting this):

I need to parse a hardware code to pick and choose just the statements starting with "$fwrite(" and that ends with a ";". The coding has new-lines in between those statements, However I want them to be chosen too There are accidental "\n" in between the arguments of $fwrite which should be considered as a single line. When I tried doing :

#!/usr/local/bin/perl -w use strict; open(IN,"<ab.in"); while($_=<IN>) { if($_=~/\$display(.*?);$/i) { print; } } close(IN);

I get statements that have nested "fwrite" statements omitted in the o/p.

Eg. For the statement below:

$fwrite(f,{few parameters},$fwrite(f,{kjg,kk})); $fwrite(f,koal,jirp); fwrite(f,jrei, orowp,jgo);
the o/p is supposed to be:
$fwrite(f,{few parameters},fwrite(f,{kjg,kk})); $fwrite(f,koal,jirp); fwrite(f,jrei,orowp,jgo);
But the o/p that my code is producing instead is

$fwrite(f,koal, jirp);

In short, it is not recognizing fwrite statements that either have: a. a '\n' between its arguments b. nested fwrites.

Hope this is clear.. Can anybody please suggest a better solution.

Thanks, Hari

Replies are listed 'Best First'.
Re: A Question on Regex
by davido (Cardinal) on Sep 07, 2011 at 07:55 UTC

    Maybe try:

    /\$fwrite[^;]+;$/i

    That way you're being specific about what you want to see come at the end of the line, and what is not permitted to come earlier.

    But I have to admit to not being 100% clear on your spec with respect to such issues as whether newlines are permitted mid-statement, and what keyword you're really using: $fwrite, $display, or something else. Your example statement might be missing a '$' on the second 'fwrite', and those sorts of things make it hard to know for sure what is needed.


    Dave

Re: A Question on Regex
by Khen1950fx (Canon) on Sep 07, 2011 at 08:03 UTC
    Have you tried re? Try this: It'll show you where you went wrong.
    #!/usr/local/bin/perl use strict; use warnings; use re qw(debug); open IN, '<', '/root/Desktop/new file'; while(<IN>) { if($_ =~ /\$display(.*?);$/i) { print $_; } } close IN;
Re: A Question on Regex
by Anonymous Monk on Sep 07, 2011 at 07:54 UTC

    I need to parse a hardware code

    Use a hardware code parser? Surely you're not the first to need to parse such a thing , whatever it is :)

    If you know the name, search cpan or sourceforge (even google) for a parser

    The coding has new-lines in between those statements, However I want them to be chosen too.

    Then you will have to read more than one line at a time, keep track of lines ... see the replies to String Search for some ways ( esp Re: String Search,Re^5: String Search )

    Also , your sample data doesn't reflect this requirement, see How do I post a question effectively?

Re: A Question on Regex
by Utilitarian (Vicar) on Sep 07, 2011 at 10:26 UTC
    not sure if this is what you require, it depends on all statements ending in a semi-colon.
    #!/usr/bin/perl while(<DATA>){ while (! /;$/){ chomp; $_ .=readline DATA; } print if /^\$fwrite.+;/; } __DATA__ $fwrite(f,{few parameters},fwrite(f,{kjg,kk})); $fwrite(f,koal,jirp); $fwrite(f,jrei,orowp, jgo); fwrite(f,jrei,orowp,jgo);
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: A Question on Regex
by Anonymous Monk on Sep 07, 2011 at 08:09 UTC
Re: A Question on Regex
by bennierounder (Sexton) on Sep 07, 2011 at 08:02 UTC
    If you are using perl 5.010 or newer then you can use say instead of print, this will automatically put \n at the end of a line. If you are using \n in the middle of a line the try appending things together with . I'm sure you'll work kit out. bennierounder

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 06:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found