I am trying to make simple regex that will check if a line is blank or not.
Case;
" some" // not blank
" " //blank
"" // blank
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The pattern you want is something like this in multiline mode:
Explanation:
^is the beginning of string anchor.$is the end of string anchor.\sis the whitespace character class.*is zero-or-more repetition of.In multiline mode,
^and$also match the beginning and end of the line.References:
A non-regex alternative:
You can also check if a given string
lineis “blank” (i.e. containing only whitespaces) bytrim()-ing it, then checking if the resulting stringisEmpty().In Java, this would be something like this:
The regex solution can also be simplified without anchors (because of how
matchesis defined in Java) as follows:API references
String String.trim()boolean String.isEmpty()trueif, and only if,length()is0.boolean String.matches(String regex)