I’m making a code which takes a file and checks if it’s a valid Java code, must be done using regexes.
I run over every line and checks if it end with ; or { or }.
however, I can’t seem to manage to limit it to only 1 occurence of it.
i.e
both
int i=0;
and
int i=0 ;;;;;;;
would pass..
The regex I’m using right now is
.+;{1}\\s*$
tried many other options, non seems to work.
any help would be great!
Thanks
Your problem is that the
.will also match a semicolon, so your expression will match all but the last semicolon as part of the.+.To avoid this, you could match any character except a semicolon by using
[^;]. i.e.However, you should be aware that
int i=0 ;;;;;;;;;is perfectly valid Java code.