I want to parse a variable declaration statement and get the variable name. I am doing the below
String var = "private String ipaddress;";
i m using the regex pattern below to match the above string
.*private\\s+([a-z]*)\\s+([a-z0-9_]*);
It does not work. It says no match found Can any one please help.
Since the declaration of a variable in Java can have more the 3 words before the variable name, I would suggest you do not limit your search and use this:
It will look for any variable name that begins with a whitespace and ends with either ";" or "=". This is a more general search of variable name.
EDIT This one got me thinking actually, since this is also legal declaration in Java:
This actually could be improved probably as it was thinked/done fast.
Have a look at Checkstyle regex patterns for naming conventions (types, methods, packages etc). More info here.
First of all, remove that dot from the beginning of the regex, since it requires a character before the
private
for a match.Second, your regex is case sensitive and won't match the capital s. Either use
[a-zA-Z]
or make the expression case insensitive ((?i)
at the start IIRC).Btw,
[a-zA-Z0-9_]
would be the same as\w
.Another thing: your expression would also catch illegal variable names as well as miss legal ones. Variables are not allowed to start with a number but they could also contain dollar signs. Thus the name expression should be something like
([a-zA-Z_$][\w$]*)
meaning the first character must be a letter, underscore or dollar sign followed by any number of word characters or dollar signs.A last note: depending on what you do with those declarations, keep in mind that you might have to check for those reserved words. The adjusted expression would still match
"private String private"
, for example.Another last note: keep in mind that there might more modifiers than
private
for a variable, e.g.public
,protected
,static
etc. - or none at all.Edit:
Now that you have the asterisk after the first dot, that shouldn't be a problem for your special case. However, a dot matches almost any character and thus would match
fooprivate
as well. Depending on what you want to achieve either remove the dot or add a\s+
after the.*
..*private\\s+(\\w*)\\s+(\\w*);
use this pattern. [a-z] is a lowercase letter, but "String" in your text starts with uppercase
S
.\\w
is a word character. It's the same as[a-zA-Z0-9_]
It seems that your texts will be like
"private <type> <field name>;"
and if it's so, your type can contain uppercase lowercase letters, numbers or underlines, so writing\\w
is a good solution.You should use this regex:
This will make sure to match:
private