betelgeuse:tmp james$ echo " method(a,b,c) "> test1
betelgeuse:tmp james$ echo " method(a,b,c) " > test3
betelgeuse:tmp james$ echo " method({a:a, b:b, c:c})" > test2
betelgeuse:tmp james$ grep "method([^{]" test*
test1: method(a,b,c)
test3: method(a,b,c)
To explain: [ ]
defines a character class - ie, the character in this position can match anything inside the class.
The ^
as the first character of the class is a negation: it means that this class matches any character except the characters defined in this class.
The {
of course is the only character we care about not matching in this case.
So in some, this will match any string that has the characters method(
followed by any character except {
.
There are other ways you could do this instead:
betelgeuse:tmp james$ grep "method(\w" test*
test1: method(a,b,c)
test3: method(a,b,c)
\w
in this case is (assuming the C locale) equivalent to [0-9A-Za-z]
. If you want to allow an optional space, you could try:
betelgeuse:tmp james$ grep "method([[:alnum:][:space:]]" test*
test1: method(a,b,c)
test3: method( a, b, c)
betelgeuse:tmp james$
(in grep syntax, [:alnum:] is the same as
\w;
[:space:]refers to any whitespace character - this is represented as
\s` in most regex implementations)