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 .* .
Thomas
source share