tags:

views:

78

answers:

3

I am trying to find out all occurences of my concrete word (method call with deprecated API) in all files in a directory. I need a regexp to find all such occurences which do not contain updated call (new API). Can you help me please?

Example:

  • deprecated api: method(a,b,c)
  • new api: method({a:a, b:b, c:c})

The regexp should find all files containing 'method' but not 'method({'.

Thank you.

+2  A: 
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)

James Polley
+1  A: 

You can use character classes to exclude a following {, e.g.

/method\([^{]/
The MYYN
besides what i've commented in my own reply, the `(` needs to be escaped in your regex.
David Hedlund
+2  A: 

I'd say the proper way is to use the negative look-ahead operator, ?!

/method(?!\(\{)/

The above states, "any occurence of method that is not followed by ({"

It meets your requirements better than the suggested /method([^{]/ as the latter does not match string end (i.e. abc abc method) and it doesn't handle the combination of two characters ({ that you requested very well.

David Hedlund
correct; my version only matches `method(` and not `method`; that's what I *think* the OP wants, but the question is a little ambiguous. If we're specifically looking for function calls, most languages require the `(`, so including it will catch function calls and not (for instance) comments that contain the word `method`
James Polley
And, to make sure that we're not thrown off by whitespace, you might use `/method(?!\s*\(\s*\{)/`.
Tim Pietzcker
I'm curious about `doesn't handle the combination of the two characters` though. Could you elaborate?
James Polley
@James: yeah, i couldn't find a good way to phrase it, but i was referring to what you mentioned in your previous comment, that it matches `method(` not followed by `{` rather than `method` not followed by `({`, if the regex really did need to find all occurences of `method` that aren't `method({`, then assuming `method(` would miss out all potential other occurences, `method_`
David Hedlund