Perl, Regular expression?

D

Davy

Hi all,

I am a Perl newbie :)
I want to extract the line from a file, which have the division symbol
"/" (something like
input1/input2 ). But I want to exclude the comment symbol "*/", "/*"
and "//"?

What's the regular expression?

Any suggestions will be appreciated!
Best regards,
Davy
 
T

Todd W

Davy said:
Hi all,

I am a Perl newbie :)
I want to extract the line from a file, which have the division symbol
"/" (something like
input1/input2 ). But I want to exclude the comment symbol "*/", "/*"
and "//"?

What's the regular expression?

Any suggestions will be appreciated!

This might work for you:

$ perl look.pl
this should / match
this / will

$ cat look.pl
use warnings;
use strict;

while ( my $line = <DATA> ) {
if ( $line =~ m#(?<![*|/])/(?![*|/])# ) {
print $line;
}
}

__DATA__
/* dont match this
this wont match either
this should / match
// but this shouldnt
this / will
but this wont */

Happy Holidays,

Todd W.
 
U

usenet

Davy said:
I want to extract the line from a file, which have the division symbol
"/" (something like
input1/input2 ). But I want to exclude the comment symbol "*/", "/*"
and "//"?

Do you mean you want to simply exclude those symbols, or do you intend
to exclude the text they exclude also? for example, do you want to
define your matches thus:

THIS MATCHES //but none of this does for the remainder of this line
THIS MATCHES */but this does not/* AND THIS MATCHES
THIS MATCHES */but this does not
and this does not
and this does not
and this does not/* AND THIS MATCHES

What is your intent with these symbols?
 
D

Davy

Hi,

Thank you for your help and sorry for not clarity.
I am a C language user.

For I want to find division symbol "/" in C source file. And I want to
exclude the line
with only comment symbol "*/" or "/*" or "//". i.e. line with both
division symbol "/" and
comment symbols should be included.

----File.c begin----
NOT MATCH /* File.c
NOT MATCH Auther: abc
NOT MATCH Date:2005*/
MATCH c = a/b;
MATCH d = e/f; //division
NOT MATCH //division
----File.c end------

Happy holiday!
Davy
 
A

Anno Siegel

Todd W said:
Davy said:
Hi all,

I am a Perl newbie :)
I want to extract the line from a file, which have the division symbol
"/" (something like
input1/input2 ). But I want to exclude the comment symbol "*/", "/*"
and "//"?

What's the regular expression?

Any suggestions will be appreciated!

This might work for you:

$ perl look.pl
this should / match
this / will

$ cat look.pl
use warnings;
use strict;

while ( my $line = <DATA> ) {
if ( $line =~ m#(?<![*|/])/(?![*|/])# ) {
print $line;
}
}

No, your character classes are wrong. You don't need "|" inside a character
class to separate alternatives. Inside [], "|" doesn't have a special
meaning. In effect you are excluding "|/" and "/|" from the set of matches.
Add "this /| should match, but doesn't" to the test data to see the effect.

m{ (?<![*/]) / (?![*/]) }x and print while <DATA>;

Anno
 
T

Tad McClellan

I am a C language user.


Everybody has problems. (heh)

For I want to find division symbol "/" in C source file.


You should have mentioned that your data was C source in your
original post. Having a good description of the data to be
processed is important in deciding how to answer the question.

Using a regular expression instead of a Real Parser will be
cumbersome, error-prone, and sometimes incorrect.

And I want to
exclude the line
with only comment symbol "*/" or "/*" or "//".


I think your Question is Asked Frequently:

perldoc -q comment

How do I use a regular expression to strip C style comments from a file?

(it ain't pretty.)

i.e. line with both
division symbol "/" and
comment symbols should be included.


I think your spec is still too loose to implement.

You say you want to _match_ lines like the below, even when
the slash is not a division symbol?

a/b */
/* a/b
//division a/b

all of those are lines with "division symbol" and "comment symbols".

----File.c begin----
NOT MATCH /* File.c
NOT MATCH Auther: abc


What about if the above line was:

Auther: a/c

here "inside" of a C comment, do you want it to match or to fail?

NOT MATCH Date:2005*/
MATCH c = a/b;
MATCH d = e/f; //division
NOT MATCH //division
----File.c end------



You might also want to have a look at _all_ of the FAQs that mention C:

perldoc -q " C "
 
A

A. Sinan Unur

Hi,

Thank you for your help and sorry for not clarity.
I am a C language user.

For I want to find division symbol "/" in C source file.

How about using the C preprocessor to get rid of the comments?

Sinan
 
T

Todd W

Anno Siegel said:
Todd W said:
Davy said:
Hi all,

I am a Perl newbie :)
I want to extract the line from a file, which have the division symbol
"/" (something like
input1/input2 ). But I want to exclude the comment symbol "*/", "/*"
and "//"?

What's the regular expression?

Any suggestions will be appreciated!

This might work for you:

$ perl look.pl
this should / match
this / will

$ cat look.pl
use warnings;
use strict;

while ( my $line = <DATA> ) {
if ( $line =~ m#(?<![*|/])/(?![*|/])# ) {
print $line;
}
}

No, your character classes are wrong. You don't need "|" inside a character
class to separate alternatives. Inside [], "|" doesn't have a special
meaning. In effect you are excluding "|/" and "/|" from the set of matches.
Add "this /| should match, but doesn't" to the test data to see the effect.

m{ (?<![*/]) / (?![*/]) }x and print while <DATA>;

Duh. I knew the "*" was literal in the character class, but still
incorrectly put a "|" in there thinking I needed to OR the items in the set.

As Anno said, putting the "|" inside the character class is wrong.

either:

if ( $line =~ m#(?<![*/])/(?![*/])# ) {

or

if ( $line =~ m#(?<!\*|/)/(?!\*|/)# ) {

seems to produce the desired results, though this still dosen't account for
lines of code that should be excluded because they are in comments as it
appears the op needs.

Using a preprocessor to filter out comments as suggested in another post
seems like a more robust solution.

Thanks for the correction,

Todd W.
 

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
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top