What is Java Regex to check "=2245" or "= 545" or "= 22", etc? -
ok, want check if string has =
first character & followed number, if there space between =
& number ok.
ok, here example,
string s="=2245"; --> return true string s="= 545"; --> return true string s="= 22"; --> return true string s="= m 545"; --> return false string s="=m545"; --> return false
so here did
if(s.matches("=[0-9]+")){ return true; }
this work if there not space between =
& number
so changed to:
if(s.matches("=\\s[0-9]+")){ return true; }
then work if there 1 space between =
& number & won't work in other cases.
so how fix it?
"=\\s*\\d+"
the *
means "zero or more repetitions", work if spaces there or not. \d
alternative way write [0-9]
.
Comments
Post a Comment