XML regular expression

Questions about XML that are not covered by the other forums should go here.
rojas
Posts: 3
Joined: Thu Jun 07, 2012 4:27 pm

XML regular expression

Post 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]
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML regular expression

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
rojas
Posts: 3
Joined: Thu Jun 07, 2012 4:27 pm

Re: XML regular expression

Post 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
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML regular expression

Post by adrian »

CASE1:

Code: Select all

^:.*?$
CASE2:

Code: Select all

^struct.*?$
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
rojas
Posts: 3
Joined: Thu Jun 07, 2012 4:27 pm

Re: XML regular expression

Post 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
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML regular expression

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply