I am trying to make simple regex that will check if a line is blank or not.
Case;
" some" // not blank
" " //blank
"" // blank
I am trying to make simple regex that will check if a line is blank or not.
Case;
" some" // not blank
" " //blank
"" // blank
The pattern you want is something like this in multiline mode:
^\s*$
Explanation:
^ is the beginning of string anchor.$ is the end of string anchor.\s is the whitespace character class.* is zero-or-more repetition of.In multiline mode, ^ and $ also match the beginning and end of the line.
You can also check if a given string line is "blank" (i.e. containing only whitespaces) by trim()-ing it, then checking if the resulting string isEmpty().
In Java, this would be something like this:
if (line.trim().isEmpty()) {
// line is "blank"
}
The regex solution can also be simplified without anchors (because of how matches is defined in Java) as follows:
if (line.matches("\\s*")) {
// line is "blank"
}
String String.trim()
boolean String.isEmpty()
true if, and only if, length() is 0. boolean String.matches(String regex)
^\s*$ won't detect all blank lines in a string, as \s* will also match \n, so runs of blank lines become one match. Whereas ^\s*?$ will match each blank line.Actually in multiline mode a more correct answer is this:
/(^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$/gm
The accepted answer: ^\s*$ does not match a scenario when the last line is blank (in multiline mode).
Edit: added a ^ toward the beginning to catch the case of lines ending with */ followed by a new line. Thanks John Henry.
*/ (end of comment) followed by an empty line).Try this:
^\s*$
\s also matches line breaks, so you won't "find" single empty lines inside a string containing successive empty lines.Full credit to bchr02 for this answer. However, I had to modify it a bit to catch the scenario for lines that have */ (end of comment) followed by an empty line. The regex was matching the non empty line with */.
New: (^(\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$/gm
All I did is add ^ as second character to signify the start of line.
The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.
[ \t]\r so the regex would be ^[ \t\r\n]*$. But ^\s*$ is better - more concise. If you don't want to match newlines, you can use \h (meaning horizontal whitespace) as in ^\h*$Here Blank mean what you are meaning.
A line contains full of whitespaces or a line contains nothing.
If you want to match a line which contains nothing then use '/^$/'.
Well...I tinkered around (using notepadd++) and this is the solution I found
\n\s
\n for end of line (where you start matching) -- the caret would not be of help in my case as the beginning of the row is a string \s takes any space till the next string
hope it helps
test_vec <- c(" some"," ","") . your solution : grepl("\\n\\s",test_vec) # [1] FALSE FALSE FALSE , the voted solution: grepl("^\\s*$",test_vec) # [1] FALSE TRUE TRUE . the voted solution gives the expected result, yours doesn't.ctrl+f in notepad++ ? In this case you can find (though not really match) the blank lines by selecting "Extended" Search mode and searching for '\n\s', if you select "Regular Expression', your string will match the same, and you can also try @polygenelubricants 's solution. The latter will really match the line, you can check and see the difference. I would suggest that you edit your answer to be more clear about what you're advising, so readers can take more value from it.My use case was to replace the empty strings specifically in the Intellij-based IDE (Android Studio). For that one, this solution worked perfectly fine:
explanation:
This regex will delete all empty spaces (blank) and empty lines and empty tabs from file
\n\s*