0

I read: Regular Expression to match string starting with "stop"

I want to search for all strings starting with a _ character using regex search in XCode (in case that matters) which I initially thought I would be able to search for with ^_ but that only returns me lines that start with _ they exclude lines with spaces.

I would like my search to return all of the following:

_foo
 _bar
  _baz

for example and exclude things like

foo_bar 
#_
  wholeWord_anotherWord

or any other string that doesnt start with '_' followed by myString (ex _myString is a desired result). Basically I'm looking for all variables following the _ naming convention.

I read: How to ignore whitespace in a regular expression subject string?

I tried "\s*^_" but that returned me only 'new line'_aString. Did I misunderstand the solution? What will give me the correct response of any _variable?

Community
  • 1
  • 1
Tai
  • 1,206
  • 5
  • 23
  • 48
  • @WiktorStribiżew that finds things like ,_bar foo_baz etc. – Tai Sep 01 '16 at 17:23
  • It matches whole words starting with _ and then having one or more letters, digits or underscores. I do not think your specifications are clear, so I just suggested something that sounds similar. It doesn't have to work for you, of course, it is just a shot in the dark. – Wiktor Stribiżew Sep 01 '16 at 17:41
  • \b_\w+ for me in xcode using regex search returns whole words that do not start with _. For example it returns me foo_bar. – Tai Sep 01 '16 at 22:37
  • Hm, does it support lokbehinds? Try `(?<!\w)_\w+` – Wiktor Stribiżew Sep 01 '16 at 22:38
  • Your solution returns _foo and /_foo. After looking at the results it lets in the desired results and results starting with /. – Tai Sep 01 '16 at 22:42
  • These results do not make sense actually. Well, you may tell the regex engine to ignore those with `/` like this: `(?<![\w/])_\w+` – Wiktor Stribiżew Sep 01 '16 at 22:44

1 Answers1

1

I initially thought I would be able to search for with ^_ but that only returns me lines that start with _ they exclude lines with spaces

Given that:

  • You say ^_ works for the lines that don't start with spaces; and
  • You wish you exclude lines such as foo_bar, (or " foo_bar" for that matter)
  • You are using the Xcode regex search box

Then I have found any of the following combinations:

  • ^\s*_
  • ^\s+?_
  • ^[:space:]*_

Will return matches for each of the following:

_foo
 _bar
  _baz

Note that the [:space:] character class accounts for all whitespace (tabs, form feeds, carriage return, etc).

You can return the entire string that starts with any of these criteria by adding (.*$) to the above regex' respectively;

  • (^\s*_\w+)(\s*)(.*$)
  • (^\s+?_\w+)(\s*)(.*$)
  • (^[:space:]*_\w+)([:space:]*)(.*$)

If separation is required then the back reference \1 would be _var and back reference \3 would be the string after the _var if separated by a whitespace.

hmedia1
  • 5,552
  • 2
  • 22
  • 27
  • How did you make the `[:space:]` POSIX character class use outside a bracket expression? It will never work as expected. `[:space:]` as such will be parsed as an NFA character class and will match a single `:`. or `s`, `p`, `a`, `c`, or `e` char. – Wiktor Stribiżew May 08 '17 at 17:14