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 ? Commented Apr 14, 2016 at 8:26
  • Well, iOS code parsing might not be a good fit for a regex. Only for some specific contents. Commented 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.

Sign up to request clarification or add additional context in comments.

6 Comments

What if there is another pair of parentheses after the LocalizedString()?
@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
I think Wiktor's use case is something like this: [_deleteButton setTitle:LocalizedString(@"Delete my Account") forState:UIControlStateNormal] ();
Well regex is not suitable for this case because it can't "count" the parenthesis.
@cansik @Adib @Wiktor, I think this is most suitable for me for now, i don't have any lines with () after LocalizedString(), thank you
|

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.