1

Im trying to find some pattern in my source files with python. This is what i code :

import os
import re

data=[]
pattern = re.compile('LocalizedString(.*)')

for dirname, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if '.m' in filename:
            with open(os.path.join(dirname, filename),'r') as f:
                for line in f:
                    matchObj = pattern.findall(line)
                    if matchObj:
                        for match in matchObj:
                            print "match : ", match

    if '.git' in dirnames:
        dirnames.remove('.git')

    if '.svn' in dirnames:
        dirnames.remove('.svn') 

I want to get all the parameter of LocalizedString() call within source code, for example :
in source code :

[_deleteButton setTitle:LocalizedString(@"Delete my Account") forState:UIControlStateNormal];

I want to get @"Delete my Account", but I got this :

match :  (@"Delete my Account") forState:UIControlStateNormal];  
2
  • Are there going to be instances where ) might be in parameter ?
    – user2705585
    Apr 14, 2016 at 8:26
  • Well, iOS code parsing might not be a good fit for a regex. Only for some specific contents. Apr 14, 2016 at 8:28

1 Answer 1

1

The problem is that you are using brackets to create a group in your pattern. But you need to specify the start and the end of the statement. Use escaped brackets for that:

LocalizedString\((.*)\)

Check it out on regex101.

6
  • 1
    What if there is another pair of parentheses after the LocalizedString()? Apr 14, 2016 at 8:22
  • @WiktorStribiżew It shouldn't matter, because the .* matches everything to the last occurrence of the next statement. Here is an example: regex101.com/r/bH4nL2/3
    – cansik
    Apr 14, 2016 at 8:25
  • 1
    I think Wiktor's use case is something like this: [_deleteButton setTitle:LocalizedString(@"Delete my Account") forState:UIControlStateNormal] ();
    – Adib
    Apr 14, 2016 at 8:27
  • Well regex is not suitable for this case because it can't "count" the parenthesis.
    – cansik
    Apr 14, 2016 at 8:30
  • 1
    @cansik @Adib @Wiktor, I think this is most suitable for me for now, i don't have any lines with () after LocalizedString(), thank you
    – dann
    Apr 14, 2016 at 8:45

Your Answer

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

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