Regex - Combining statements

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to create one RegEx pattern, something like,
\s*(?<typename>\S*){1}\s*(?<varname>\S*){1}\s*[;|=]{1}\s*(?<varvalue>.*)[;]*
out of the following 2 expressions and am not having much luck.

Expressions
-----------------------------
(?<typename>\S*){1}\s*(?<varname>\S*){1}\s*[;]{1}
(?<typename>\S*){1}\s*(?<varname>\S*){1}\s*=\s*(?<varvalue>.*)[;]


Text to parse
-----------------------------
public type.subtype _myvar =data.subdata;
private int v;
private int x=5;


Expected output
-----------------------------

typename varname varvalue
============================================
type.subtype _myvar data.subdata
int v
int x 5


Any help is appreciated.
Dave
 
Dave said:
I am trying to create one RegEx pattern, something like,
\s*(?<typename>\S*){1}\s*(?<varname>\S*){1}\s*[;|=]{1}\s*(?<varvalue>.*)[;]*
out of the following 2 expressions and am not having much luck.

You don't need to specify {1}, thats the default.
You don't need [;], just use ; for single characters.

Try that, looks like it does it (for the provided samples):

(?<typename>\S+)\s*(?<varname>\S+?)\s*(=\s*(?<varvalue>.*?))?\s*;

hth,
Max
 

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

Back
Top