Home: Perl Programming Help: Regular Expressions:
regular expression to iggnore



devi
Novice

Jun 13, 2012, 11:00 AM


Views: 24657
regular expression to iggnore

^\s*case\s+.*?:\s*$

My tool reads a xml file to parse the soruce code files (like java, C++ etc..) to generate the line count, the above expression in my xml file makes all the lines which has only the word case : to get ignored. Could someone plese help me in below two cases to write above kind of expression.

CASE1:- If any line starts with : (colon), then that line should be ingored. For example, in the below line the word END stars with : , so this line should be ignored. (there is no space betwwen : and END word)

:END

CASE 2:- if any line starts with the word struct then that line should be ignroed. for example, below line should be ingnored

struct simple


rovf
Veteran

Jun 14, 2012, 12:25 AM


Views: 24620
Re: [devi] regular expression to iggnore

From perldoc perlre:


^ Match the beginning of the line

By default, the "^" character is guaranteed to match only the beginning
of the string, the "$" character only the end (or before the newline at
the end), and Perl does certain optimizations with the assumption that
the string contains only one line. Embedded newlines will not be matched
by "^" or "$". You may, however, wish to treat a string as a multi-line
buffer, such that the "^" will match after any newline within the string
(except if the newline is the last character in the string), and "$"
will match before any newline. At the cost of a little more overhead,
you can do this by using the /m modifier on the pattern match operator.


Hence, if the expression you want to test by regex, contains one line of your source file, you can use ^ to anchor your string at the beginning of the line.

However, I wonder why you are asking this, because you are using ^ already in the regexp you had posted.

BTW, your expression
^\s*case\s+.*?:\s*$ does not do what you claim it would do. It matches not only thos lines which have only the word case :, but would, for example, also match a line such as


Code
  case 35 :



devi
Novice

Jun 14, 2012, 11:51 AM


Views: 24574
Re: [rovf] regular expression to iggnore

thaks for your reply.

<expression>^\s*}\s+else\s+{.*$</expression>

i have written above regular expression to ignore lines which are in below format and It is working as expected

} else {

But, if add anything after "else" word, it is still ignoring the line. for example, it should not ignore below line.

} else if (2>3) {

could you please correct it

} else { ------ This line should be ignored

} else if (2>3) { ------ This line should not be ignored


rovf
Veteran

Jun 15, 2012, 3:36 AM


Views: 24555
Re: [devi] regular expression to iggnore


Code
my $ignore_this_line = /^\s*\}\s*else\s*\{\s*/;


Of course, this also would ignore the line


Code
                   }else           {


and it would not ignore the line


Code
  } else {  /* This can be ignored */



devi
Novice

Jun 15, 2012, 3:46 AM


Views: 24553
Re: [rovf] regular expression to iggnore

But it is ignoring below line also, this I do not want as part of my requirement.

} else if (a<10) {

It should ignore, only below kind of lines

} else {

} else {

(This post was edited by devi on Jun 15, 2012, 3:48 AM)


rovf
Veteran

Jun 15, 2012, 5:06 AM


Views: 24547
Re: [devi] regular expression to iggnore


Quote
But it is ignoring below line also, this I do not want as part of my requirement.

} else if (a<10) {


Not true. The regexp


Code
/^\s*\}\s*else\s*\{\s*/



does not match


Code
} else if (a<10) {


Having said this, there is a bug in my regexp: I was missing the final $. The correct regexp has of course to be


Code
/^\s*\}\s*else\s*\{\s*$/



devi
Novice

Jun 15, 2012, 6:05 AM


Views: 24546
Re: [rovf] regular expression to iggnore

your regular expression is working, but it is ignoring the lines like these also ----> } else if(a>10) {

One working case:-

^\s*else\s+{.*$

i have added above line for a different case where lines like " else { " should be ignored and it is working pretty fine as well. For example:

else { ---------- this line is getting ignored.

else if (a>10) { ------------ This line is not getting ignored


rovf
Veteran

Jun 15, 2012, 6:47 AM


Views: 24543
Re: [devi] regular expression to iggnore


Code
your regular expression is working, but it is ignoring the lines like these also ----> } else if(a>10) {


I don't think that what you say here is correct. Please demonstrate your claim by a short, self-contained example program.

Regards,

Ronald


rovf
Veteran

Jun 15, 2012, 7:03 AM


Views: 24539
Re: [devi] regular expression to iggnore

BTW, here you can see that it *is* working as expected:


Code
$ perl -lwe 'print("$_ : ",/^\s*\}\s*else\s*\{\s*$/ ? "match" : "no match") for ("} else { ", "} else if (a<10) { ")' 
} else { : match
} else if (a<10) { : no match