Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex - Not an empty string, only numbers, 8 chars long [closed]

Tags:

java

regex

I'm trying to construct a regex that basically allows only numbers, 8 characters long and cannot be empty ie "" or have 8 blank spaces

I've been able to get two separate regex that will nearly do what I'm after: ^(?!\s*$).+ which does not allow empty strings, but permits white space. Also: ^[0-9]+$ which lets me only search for numbers.

I would like to combine these regex expression and also and in a clause to match strings that are 8 characters long.

Any advice on how I could combine what I have so far?

like image 388
deanmau5 Avatar asked Dec 27 '22 02:12

deanmau5


1 Answers

Just place ^(?!\s*$) at start of your regex. Try this way ^(?!\s*$)[0-9\s]{8}$?

  • ^(?!\s*$) as you know will check if entire string is not only white spaces
  • [0-9\s] will match any digit and white space
  • {8} means exactly 8 occurrences of element before it (in our case digit or white space)
like image 100
Pshemo Avatar answered Mar 09 '23 00:03

Pshemo