Little question on regex.

Discussion in 'Perl Misc' started by NeoAsimov, Oct 28, 2004.

  1. NeoAsimov

    NeoAsimov Guest

    Hello,

    I want to select specific lines of C++ source codes.


    There is the pattern that I want to match:

    I want a line that have a "..." string in but not any "//" characters.

    Exemple:

    public void Foo("test") [This one match]
    // public void Foo("test") [This one doesn't match]



    I tested many things but none works....

    there are some of my tests:

    ^([^//])*("[^"]*")$
    ^([^//]|[^"])* "[^"]*"
    ^([^//]*|[^"]*)*"[^"]*"
    ^([^//]*|[^"]*|$)("[^"]*")
    ^[ \t]*[^//]*("[^"]*")*$

    Is this really possible to do what i want?




    Thank alot for your help/hints,


    Salutations,
    NeoAsimov, Oct 28, 2004
    #1
    1. Advertising

  2. NeoAsimov

    Paul Lalli Guest

    "NeoAsimov" <> wrote in message
    news:...
    > I want to select specific lines of C++ source codes.
    >
    > There is the pattern that I want to match:
    >
    > I want a line that have a "..." string in but not any "//"

    characters.
    >
    > Exemple:
    >
    > public void Foo("test") [This one match]
    > // public void Foo("test") [This one doesn't match]


    > I tested many things but none works....
    >
    > there are some of my tests:
    >
    > ^([^//])*("[^"]*")$
    > ^([^//]|[^"])* "[^"]*"
    > ^([^//]*|[^"]*)*"[^"]*"
    > ^([^//]*|[^"]*|$)("[^"]*")
    > ^[ \t]*[^//]*("[^"]*")*$


    You seem to be using character classes where none are called for. Have
    you read
    perldoc perlretut
    and
    perdoc perlre
    ? Those are good places to start.

    If all you're looking for is to match strings which do not contain //,
    you can do so like this:

    if ($string !~ m|//| ){
    print "No comment found!";
    }

    If you're looking for something more advanced (comments in particular
    positions, for example), you need to give us a better description of
    your problem.


    > Is this really possible to do what i want?


    I'm sure it is. But you really haven't described well enough what it is
    you want.
    Paul Lalli, Oct 28, 2004
    #2
    1. Advertising

  3. NeoAsimov

    Laura Guest

    NeoAsimov wrote:

    > Hello,
    >
    > I want to select specific lines of C++ source codes.
    >
    >
    > There is the pattern that I want to match:
    >
    > I want a line that have a "..." string in but not any "//" characters.
    >
    > Exemple:
    >
    > public void Foo("test") [This one match]
    > // public void Foo("test") [This one doesn't match]
    >
    >
    >
    > I tested many things but none works....
    >
    > there are some of my tests:
    >
    > ^([^//])*("[^"]*")$
    > ^([^//]|[^"])* "[^"]*"
    > ^([^//]*|[^"]*)*"[^"]*"
    > ^([^//]*|[^"]*|$)("[^"]*")
    > ^[ \t]*[^//]*("[^"]*")*$
    >
    > Is this really possible to do what i want?
    >
    >
    >
    >
    > Thank alot for your help/hints,
    >
    >
    > Salutations,


    Try this:

    #! /usr/bin/perl

    $string = $ARGV[0];
    if (!($string =~ m/\/\//))
    {print "match!\n";}
    else
    {print "no match.\n";}
    Laura, Oct 28, 2004
    #3
  4. NeoAsimov

    Uri Guttman Guest

    >>>>> "L" == Laura <> writes:

    >> There is the pattern that I want to match:
    >>
    >> I want a line that have a "..." string in but not any "//" characters.
    >>
    >> Exemple:
    >>
    >> public void Foo("test") [This one match]
    >> // public void Foo("test") [This one doesn't match]
    >>


    L> $string = $ARGV[0];
    L> if (!($string =~ m/\/\//))
    L> {print "match!\n";}
    L> else
    L> {print "no match.\n";}

    huh? that doesn't even come close to what the OP asked for. where is the
    match for '...' (i assume the OP means some arbitrary string
    there). then your logic is backwards with print "match" when the match
    fails. finally you should use alternate delimiters when you need to
    match / chars. 3 strikes and yer out! (just like the cardinals).

    so when will you decide to actually learn perl? you seem to have the
    interest but haven't mastered even the basics. i recommend you read a
    few decent perl books and more of the docs before you embarrass yourself
    again here.

    uri
    Uri Guttman, Oct 28, 2004
    #4
  5. NeoAsimov

    Laura Guest

    Uri Guttman wrote:

    >>>>>> "L" == Laura <> writes:

    >
    > >> There is the pattern that I want to match:
    > >>
    > >> I want a line that have a "..." string in but not any "//"
    > >> characters.
    > >>
    > >> Exemple:
    > >>
    > >> public void Foo("test") [This one match]
    > >> // public void Foo("test") [This one doesn't match]
    > >>

    >
    > L> $string = $ARGV[0];
    > L> if (!($string =~ m/\/\//))
    > L> {print "match!\n";}
    > L> else
    > L> {print "no match.\n";}
    >
    > huh? that doesn't even come close to what the OP asked for. where is the
    > match for '...' (i assume the OP means some arbitrary string
    > there). then your logic is backwards with print "match" when the match
    > fails. finally you should use alternate delimiters when you need to
    > match / chars. 3 strikes and yer out! (just like the cardinals).


    I hate to say it, but my solution does work. As a complete Perl script, you
    can cut&paste and run it using the 'OP's exact example and they will match
    and not match exactly as he requested. My 'backwards' logic leads to the
    exact logical solution that was requested. Not that it is the only or the
    best solution, but it is a correct solution nonetheless. Just because I am
    a beginner in Perl (which I do not put down as you previously claimed and I
    happen to like very much) and I am not a world renown, published Perl
    hacker like yourself, does not mean that I do not have a brain and that I
    cannot learn and come up with a workable solution in very simple cases such
    as this. I hate to have to respond like this in a thread that involves an
    honest question and an honest answer, but you started it. I would have
    hoped to see the experts make constructive suggestions and propose more
    advanced alternatives. Sometimes it may be constructive to see the
    beginner's solution as well as the expert's. I may have been wrong on
    other points I have made in the past, but you should judge my answer here
    on its own merits.

    I think the m/\/\// looks pretty and I'm sure that if a newcomer like me can
    get it, most Perl programmers will have no problem identifying its meaning.

    >
    > so when will you decide to actually learn perl? you seem to have the
    > interest but haven't mastered even the basics. i recommend you read a
    > few decent perl books and more of the docs before you embarrass yourself
    > again here.
    >
    > uri
    Laura, Oct 29, 2004
    #5
  6. NeoAsimov

    Uri Guttman Guest

    >>>>> "L" == Laura <> writes:

    L> I hate to say it, but my solution does work. As a complete Perl
    L> script, you can cut&paste and run it using the 'OP's exact example
    L> and they will match and not match exactly as he requested. My
    L> 'backwards' logic leads to the exact logical solution that was
    L> requested. Not that it is the only or the best solution, but it is
    L> a correct solution nonetheless. Just because I am a beginner in

    it isn't correct. he wanted to match a string in lines that didn't
    contain //. your code just matched // or not. not much of backwards
    logic nor forwards logic.

    L> Perl (which I do not put down as you previously claimed and I
    L> happen to like very much) and I am not a world renown, published
    L> Perl hacker like yourself, does not mean that I do not have a brain
    L> and that I cannot learn and come up with a workable solution in
    L> very simple cases such as this. I hate to have to respond like
    L> this in a thread that involves an honest question and an honest
    L> answer, but you started it. I would have hoped to see the experts
    L> make constructive suggestions and propose more advanced
    L> alternatives. Sometimes it may be constructive to see the
    L> beginner's solution as well as the expert's. I may have been wrong
    L> on other points I have made in the past, but you should judge my
    L> answer here on its own merits.

    you have not shown interest in listening to others and continue to spout
    FUD. plenty of beginners here try to help out but not many spew stuff
    like perl isn't compiled, and c is always faster and other nonsense.


    L> I think the m/\/\// looks pretty and I'm sure that if a newcomer
    L> like me can get it, most Perl programmers will have no problem
    L> identifying its meaning.

    it is called leaning toothpick syndrome and it is why larry made sure
    all quotelike operators could take alternative delimiters.

    see, i am trying to teach you something. you decided it wasn't worth
    it. so i decide your posts aren't worth much. i follow up your posts to
    make sure others who read them later (via google) will see better
    answers or at least refutations of yours.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Oct 29, 2004
    #6
  7. (NeoAsimov) writes:
    > I want to select specific lines of C++ source codes.
    >
    > There is the pattern that I want to match:
    >
    > I want a line that have a "..." string in but not any "//" characters.
    >
    > Exemple:
    >
    > public void Foo("test") [This one match]
    > // public void Foo("test") [This one doesn't match]
    >


    Could it be that you want to see all occurrences of C++ strings? In
    that case, you need to distinguish between the two cases:

    > // public void Foo("test")


    and

    > public void Foo("test") // this is my test function


    A tricky case is when a string contains "//"; then you do not want to
    treat the sequence as a comment starter.

    This is a sort of solution, but doesn't handle the tricky case:

    #! /usr/bin/perl -w

    use strict;
    use diagnostics;

    while (<>) {
    s!//.*!!;
    print if (/".*"/);
    }

    It first removes anything after "//", then prints the line if it contains
    at least two double quotes.
    This also doesn't handle the case if a string is started on one line
    and ended on the next. Depending on what you use this for, you may prefer
    the program to err in one direction or the other (displaying too few
    or too many).
    Arndt Jonasson, Oct 29, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ivan Marsh

    A little regex help?

    Ivan Marsh, Jul 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,715
    JamesW
    Jul 4, 2003
  2. Fulvio

    [OT] a little about regex

    Fulvio, Oct 18, 2006, in forum: Python
    Replies:
    9
    Views:
    287
    Fulvio
    Oct 20, 2006
  3. ThaDoctor
    Replies:
    3
    Views:
    380
    Alan Woodland
    Sep 28, 2007
  4. Replies:
    3
    Views:
    746
    Reedick, Andrew
    Jul 1, 2008
  5. Daniel
    Replies:
    1
    Views:
    208
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page