Page 1 of 1

XML regular expression

Posted: Thu Jun 07, 2012 4:32 pm
by rojas
One of the tool we use reads a XML file to generate the number of lines of code from given source files types (like.. java,sql etc..). Below is the line of code wirtten in the XML file which is used to ignore the lines which have only the words "do" or "while".

<codeArea name="Begin/End tags" isCode="false" >
<expression>^\s*begin\s*$</expression>
<expression>^\s*end\s*$</expression>
</codeArea>


In the same way, how can I write the code to ignore the "case" statements which is used with "switch" in programming languages. For example, in the below code i want to ignore "case" statements


switch ((char)(e.KeyChar))

{
case '\b':
case "Thr":
case '1':
}

I tried below way, but it doesn't worked. could someone please help on it.


<expression>^\s*case[*]:\s*$</expression>[/b]

Re: XML regular expression

Posted: Thu Jun 07, 2012 5:58 pm
by adrian
Hi,

I'm not sure what type of regular expressions are you using. From seeing ^ and $, I'm guessing they are Perl/Java like.

The regular expression would look like this:

Code: Select all

^\s*case\s+.*?:\s*$
If ".*?" is not accepted, use ".*".

Regards,
Adrian

Re: XML regular expression

Posted: Wed Jun 13, 2012 4:43 pm
by rojas
^\s*case\s+.*?:\s*$

The above expression you given worked pretty well. Is it perl syntax? Could you plese help me in below two cases.

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

Re: XML regular expression

Posted: Thu Jun 14, 2012 11:27 am
by adrian
CASE1:

Code: Select all

^:.*?$
CASE2:

Code: Select all

^struct.*?$
Regards,
Adrian

Re: XML regular expression

Posted: Thu Jun 14, 2012 9:38 pm
by rojas
<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

Re: XML regular expression

Posted: Fri Jun 15, 2012 11:21 am
by adrian
You're not using the wildcard in the right place. You should use it before the left curly bracket({), because that's where you have the "if".
<expression>^\s*}\s+else\s+.*?{$</expression>
Note that for most languages the spaces are optional near curly brackets. So you may want to use \s* instead of \s+.

Also note that for some (if not most) regular expression engines the curly, square and round brackets are special characters and they should be escaped when used as literals in an expression.
e.g. \{

Regards,
Adrian