I am trying to use a regex to search/replace a pattern / substitution / in vim.

I have a Java file which is trying to check if a weather data variable is set and if it is do one thing, if not omit it from being processed into JSON format. I wrote a method in a different class ( called StumpJunk.isSet - works just like PHP isset by checking if null or empty)

The old way I was doing it was if (varName != null)

The new pattern I want it to be is if (StumpJunk.isset(varName))

I have tried multiple regex methods in VIM to no luck:

:%s/if (\(.*\)) != null)/(StumpJunk.isSet(\1))/g
:%s/if \(\(.*\)\) \!\= null\)/\(StumpJunk.isSet\(\1\)\)/g
:%s/if \v\((.*)\) \!\= null\)/(StumpJunk.isSet(\1))/g
:%s/if \v\((.*)\) \!\= null\)/(StumpJunk.isSet("\1")/g

Every time it comes back saying pattern not found:

Pattern not found: \vif\((.*) \!\= null\) 

You have a ) too many in the first pattern, and you lack if in the replacement:

:%s/if (\(.*\) != null)/if (StumpJunk.isSet(\1))/g   
  • or :%s/\vif \((.*) \!\= null\)/if (StumpJunk.isSet(\1))/g if you want to use very-magic regex mode. – Mark Perryman Sep 7 '17 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.