Regex to match variable declaration in java

I want to parse the variable declaration statement and get the variable name. I do below

String var = "private String ipaddress;"; 

im using the regex pattern below to match the above line

 .*private\\s+([az]*)\\s+([a-z0-9_]*); 

This does not work. He says no match has been found. Can anyone help.

+7
source share
5 answers

First of all, remove this point from the beginning of the regular expression, since it requires the private character to match.

Secondly, your regular expression is case sensitive and will not match capital. Either use [a-zA-Z] , or make the case insensitive to case ( (?i) at the beginning of IIRC).

Btw, [a-zA-Z0-9_] will be the same as \w .

Another thing: your expression will also capture illegal variable names, as well as skip legit ones. Variables cannot begin with a number, but they can also contain dollar signs. Thus, the name expression should be something like ([a-zA-Z_$][\w$]*) , meaning that the first character must be a letter, underscore or dollar, followed by any number of word characters or dollar signs.

Last note: depending on what you are doing with these ads, keep in mind that you may have to check these reserved words. The adjusted expression will still match "private String private" , for example.

One more note: keep in mind that there can be more modifiers for a variable than private , for example. public , protected , static , etc. - or nothing at all.

Edit:

Now that you have an asterisk after the first dot, this should not be a problem for your special case. However, the dot matches almost any character and thus will match fooprivate . Depending on what you want to achieve, delete the point or add \s+ after .* .

+10
source

Since a variable declaration in Java can contain more than three words before the variable name, I would suggest that you not restrict the search and use this:

 String var = "private String ipaddress;"; //String var2 = "private static final int test=13;"; Pattern p = Pattern.compile(".+\\s(.+?)(;|=)"); Matcher m = p.matcher(var); while(m.find()){ System.out.println(m.group(1)); } 

It will look for any variable name that starts with a space and ends with either ";" or "=". This is a more general search for a variable name.

EDIT . This made me wonder, since this is also a legal declaration in Java:

 private static volatile String s , t1 = ""; 

In fact, this could be improved, perhaps in the way it was accepted / done quickly.

 public static void main(String[] args) { String var0 = "private static final int test,test2;"; String var1 = "private \n static \n final \n int \n testName \n =\n 5 \n"; String var2 = "private \n static \n final \n String \n testName \n =\n \" aaa = bbbb \" \n"; String var3 = "private \n static \n final \n String \n testName,testName2 \n =\n \" aaa = bbbb \" \n"; String var4 = "int i;"; String var5 = "String s ;"; String var6 = "final String test ; "; String var7 = "public int go = 23;"; String var8 = "public static final int value,valu2 ; "; String var9 = "public static final String t,t1,t2 = \"23\";"; String var10 = "public \n static \n final \n String s1,s2,s3 = \" aaa , bbb, fff, = hhh = , kkk \";"; String var11 = "String myString=\"25\""; LinkedList<String> input = new LinkedList<String>(); input.add(var0);input.add(var1);input.add(var2);input.add(var3);input.add(var4);input.add(var5); input.add(var6);input.add(var7);input.add(var8);input.add(var9);input.add(var10); input.add(var11); LinkedList<String> result = parametersNames(input); for(String param: result){ System.out.println(param); } } private static LinkedList<String> parametersNames(LinkedList<String> input){ LinkedList<String> result = new LinkedList<String>(); for(String var: input){ if(var.contains("\n")) var = var.replaceAll("\n", ""); var = var.trim(); if(var.contains("=")){ var = var.substring(0, var.indexOf("=")).trim() + ""; Pattern p = Pattern.compile(".+\\s(.+)$"); Matcher m = p.matcher(var); if(m.find()){ if(m.group(1).contains(",")){ String [] tokens = m.group(1).split(","); for(String token : tokens){ result.add(token); } } else{ result.add(m.group(1)); } } } else{ Pattern p = Pattern.compile(".+\\s(.+?)(;|=)"); Matcher m = p.matcher(var); if(m.find()){ if(m.group(1).contains(",")){ String [] tokens = m.group(1).split(","); for(String token : tokens){ result.add(token); } } else{ result.add(m.group(1)); } } } } return result; } 
+5
source

.*private\\s+(\\w*)\\s+(\\w*);
use this template. [az] is a lowercase letter, but "String" in the text begins with an uppercase S \\w is a word character. This is the same as [a-zA-Z0-9_]
It seems your texts will look like "private <type> <field name>;" , and if so, your type may contain uppercase letters, numbers or underscores, so writing \\w is a good solution.

+3
source

You should use this regex:

 ^(?s)\\s*private\\s+(\\w+)\\s+(\\w+)\\s*;\\s*$ 

This will match:

  • Case insensitive except for the private keyword
  • Multiple Line Declaration
  • spaces at the beginning, end and middle
+3
source

Check out Checkstyle regex patterns for naming conventions (types, methods, packages, etc.). More details here .

+3
source

All Articles