I am wondering whether there is a way to represent a character class that matches nothing. Could anybody let me know whether there a way? Thanks.
$ grep '[]' <<< a
grep: Unmatched [, [^, [:, [., or [=
I am wondering whether there is a way to represent a character class that matches nothing. Could anybody let me know whether there a way? Thanks.
$ grep '[]' <<< a
grep: Unmatched [, [^, [:, [., or [=
It is possible to do this in Java. Java's Pattern
class allows you to create a character class that is the intersection of two other character classes. So, if I create two character classes with no common characters and I take their intersection, then I have created a character class that effectively matches nothing. Consider the following code example.
String input = "abcdefghijklmnopqrstuvwxyz";
Pattern unPattern = Pattern.compile("[a-c&&[d-f]]");
Matcher unMatcher = unPattern.matcher(input);
System.out.println("Starting matching...");
while (unMatcher.find()) {
System.out.println("Matched " + unMatcher.group());
}
System.out.println("Ending matching.");
In the above example, I have one character class matching 'a', 'b', and 'c'. I have a second character class matching 'd', 'e', and 'f'. I intersect them using the &&
operator. Since there are no common characters, this regex will not match anything. That being said, I have no idea what use this might have. But it is possible.
Posix regexes don't offer that possibility because a ]
is taken to be a literal ]
if it appears immediately after the [
or [^
which starts the class. (The same is true for -
.)
Note that in a Posix regex, \
does not have any special significance inside a character class, so grep -E '[\s]
matches either a backslash or a lower-case s
, and nothing else. (That's not very relevant to your question but it is relevant to some other answers.)
GNU grep does implement some extensions to Posix regex, including recognising some non-standard backslash escape sequences outside of character classes. (With an emphasis on some. It doesn't recognise \d
, for example, which sometimes comes as a surprise.) But it's basically a Posix implementation, so while grep -E '\s'
does match any line including a whitespace character, grep -E '[\s]'
matches any line with either a \
or an s
.