Regex, check if a line is blank or not?

The pattern you want is something like this in multiline mode.

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. References regular-expressions. Info/Anchors, Character Classes, and Repetition A non-regex alternative 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" API references String String.trim() Returns a copy of the string, with leading and trailing whitespace omitted.

Boolean String.isEmpty() Returns true if, and only if, length() is 0. Boolean String. Matches(String regex) Tells whether or not this (entire) string matches the given regular expression.

Thank you @polygenelubricants – Adnan Jun 10 '10 at 8:36 1 @Adnan: take note of Bart's comment in Marcelo's answer; depending on how you want to handle multiple blank lines, the pattern may change slightly. – polygenelubricants Jun 10 '10 at 8:37 Well I am reading a file from Java, line by line, so I assume that this will be ok. – Adnan Jun 10 '10 at 8:42 that seems brilliant with line.

Trim :D – Adnan Jun 10 '10 at 8:54 1 excellent the codes now executes from 1.6sec to >1sec Thank you. – Adnan Jun 10 '10 at 8:57.

1 @Adnan, note that \s also matches line breaks, so you won't "find" single empty lines inside a string containing successive empty lines. – Bart Kiers Jun 10 '10 at 8:34.

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.

I'd at least change the single space with the class \t – Bart Kiers Jun 10 '10 at 8:35 @Bart K: Thx ,updated – soulmerge Jun 10 '10 at 8:36.

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 '/^$/'.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions