Home > Net >  Is there a Regex to search for all arrays that are present in a project?
Is there a Regex to search for all arrays that are present in a project?

Time:02-17

I am working on a large project in Xcode. I'm wanting to search, using the Find Navigator (See Below), for all arrays regardless of their name. I only care about any array that has this format, someArray[index].

enter image description here

Some Examples That Should Match

  • people[12]
  • section[0].rows[0]

Should Not Match

  • people[index]
  • section[section].row[row]

The regex should only return arrays, it should not return any dictionaries or other types that are not a subscripted array.

Why am I doing this? Well, it appears there have been some issues within our app where devs have not properly handled index out of bounds errors or nil values. There are far too many arrays for me to manually go through line by line to find them, so this is the best option I've come up with and it may not even be possible. If anyone has other recommendations, please feel free to share.

CodePudding user response:

You can create a regex to match any word followed by another word with optional period enclosed by brackets. Something like:

 \w \[\w (\.\w )?\]

For more info about the regex above you can check this enter image description here

For numbers only use \d instead:

\w \[\d \]

enter image description here For more info about the regex above you can check this link

  • Related