开发者

Can you decipher this regex pattern

I am trying to read someone else's code and I came across th开发者_运维技巧is piece of regex. Can anyone tell whats the author trying to do ?

if(str.matches("^\\s*$") && !str.matches("^\\s*#.*")) {
       // do something
}


The first regex matches any line that contains exclusively blank characters (e.g. spaces and tabs).

^ -- start of line
\\s -- interpolated to \s, a blank character
* -- any number of
$ -- end of line

The second regex seems redundant, as it would match a line that starts with a hash, not including leading blanks. Such a line would not match the first regex anyway.

Update: My guess is that the author intended to negate the first regex, too. That would run the body of the if for lines that do not look like comments.


The first pattern looks for an empty line - a line containing only white space (spaces, tabs, etc).

The second pattern looks for lines starting with white space followed by a # (hash), and ignores them.

It's odd; anything that matches the first regex won't match the second, so the second test is pointless. To make sense, the test should probably be:

if (!str.matches("^\\s*$") && !str.matches("^\\s*#.*")) {
       // do something with non-blank, non-comment lines
}

Or:

if (str.matches("^\\s*$") || str.matches("^\\s*#.*")) {
       // do something with all blank or all comment lines (like ignore them)
}


The first regex returns true if the string consists solely of whitespace. The second checks that it is not some string with a # (and spaces before, and random stuff after) in it, but this never happens, since the string consists of only spaces. This looks like a bug...


This only checks for a string consisting entirely of whitespace (or the empty string). The second regex serves no function, since any string matching the first regex will not match the second one.


str.matches("^\\s*$") says "is the string either empty or only contain whitespace" and !str.matches("^\\s*#.*") says "the string doesn't match some leading whitespace, followed by a #, followed by 0 or more of any other character", which is basically to say "does the string not have a leading # character".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜