Search code examples
iosregexxcodesearchxcode4

XCode search: How create a Matches Regex (regular expression) to select the correct files


I have a projec with a lot of target in XCode. I want to customize my find and select only files of one target.

My program have files code in this way:

  1. Global files that all targets use
  2. Global for devices (iPad or iPhone)
  3. Specific for that target

So, for case 1, the file names is in this way:

SCName1.m
SCOtherGlobal.m

Case 2 is:

SCiPhoneFile1.m
SCiPhoneFile2.m
SCiPadFile.m
SCiPadFile2.m

Case 3:

SCiPhoneAAFile1.m
SCiPadABFile1.m
SCiPadABOtherFile.m

So, for one target, I want to create a regular expression that search files with:

 SC* OR SCiPhone* OR SCiPhoneAA*

The more complete way is

(SC* AND NOT(SCiPhone*) AND NOT(SCiPad*)) OR 
(SCiPhone* AND NOT(SCiPad*) AND NOT(SCiPhoneAB*)) OR
(SCiPhoneAA*)

I am newbie in regular expression and my expression isn't working. My logic is correct? Someone know create the correct regular expression?


Solution

  • Since your desire is to match file names that have common elements, I think the most efficient way (if not the only way) to do this is to use exclusion (that is, files that do not pertain to the iPad in any way, or files that do not pertain to the iPhoneAB device, but any others are fine.

    So, for these inputs:

    SCName1.m
    SCOtherGlobal.m
    SCiPhoneFile1.m
    SCiPhoneFile2.m
    SCiPadFile.m
    SCiPadFile2.m
    SCiPhoneAAFile1.m
    SCiPhoneABFile2.m
    SCiPadABFile1.m
    SCiPadABOtherFile.m
    

    I can match these files:

    SCName1.m
    SCOtherGlobal.m
    SCiPhoneFile1.m
    SCiPhoneFile2.m
    SCiPhoneAAFile1.m
    

    Using this expression (which uses negative look-ahead):

    SC(?!iPad|iPhoneAB)[^.]*\.m
    

    I hope this works for you! Also, I can break this down for you, if you'd like :D