Little question on regex.

N

NeoAsimov

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,
 
P

Paul Lalli

NeoAsimov said:
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.
 
L

Laura

NeoAsimov said:
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";}
 
U

Uri Guttman

L" == Laura said:
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
 
L

Laura

Uri said:
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.
 
U

Uri Guttman

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
 
A

Arndt Jonasson

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top