2

I've got to rename our application and would like to search all strings in the source code for the use of it. Naturally the app name can appear anywhere within the strings and the strings can span multiple lines which complicates things.

I was using (["'])APP_NAME to find instances at the start of strings but now I need a more complete solution.

Essentially what I'd like to say is "find instances of APP_NAME enclosed by quotes" in regex speak.

I'm searching in Xcode in case anyone has any Xcode-specific alternatives...

5
  • Try (["'])APP_NAME\1 (to find 'APP_NAME' and "APP_NAME") Commented Jan 30, 2017 at 18:03
  • Thanks, though that still only finds instances of APP_NAME that immediately follows a " Commented Jan 30, 2017 at 18:30
  • Aha, then try (["'])(?:(?!\1).)*APP_NAME.*?\1 Commented Jan 30, 2017 at 18:31
  • Or "[^"]*APP_NAME[^"]*"|'[^']*APP_NAME[^']*' Commented Jan 30, 2017 at 18:38
  • Yep! That seems to work. Thanks! Commented Jan 30, 2017 at 18:39

1 Answer 1

2

You may use

"[^"]*APP_NAME[^"]*"|'[^']*APP_NAME[^']*'

See the regex demo.

Note that this regex is based on alternation (| means OR) and negated character classes ([^"]* matches any 0+ chars other than ").

Or, alternatively:

(["'])(?:(?!\1).)*APP_NAME.*?\1

See this regex demo. The pattern is a bit trickier:

  • (["']) - captures " or ' into Group 1
  • (?:(?!\1).)* - any 0+ occurrences of a char that is not equal to the one captured into Group 1
  • APP_NAME - literal char sequence
  • .*? - any 0+ chars other than line break chars but as few as possible`up to the first occurrence of...
  • \1 - the value captured into Group 1.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.