简体   繁体   中英

Regex (searching for function(@“string content”) to get “string content”

I have a little regex problem (don't we all sometimes). The few pieces of code are from Objective C but regex expressions are still the same I believe.

I have two functions called

NSString * CRLocalizedString(NSString *key)
NSString * CRLocalizedArgString(NSString *key, ...)

These are scattered around my project for localisation. Now I want to find them all. Well go to directory, parse all files, etc All fine there. The regexes I use on the files are

[NSRegularExpression regularExpressionWithPattern:@"CRLocalizedString\\(@\\\"[^)]+\\\"\\)" options:0 error:&error];
[NSRegularExpression regularExpressionWithPattern:@"CRLocalizedArgString\\([^)]+\\)" options:0 error:&error];

And this works perfect except that my terminates character is an ).

The problem occurs with function calls like this

CRLocalizedString(@"Happy =), o so happy =D");
CRLocalizedArgString(@"Filter (%i)", 0.75f);

The regex ends the string at "Filter (%i" and at "Happy =)". And this is where my regex knowledge ends and I do not now what to do anymore. I thought using ");" as an end but this isn't always the case.

So I was hoping someone here knew something for me (complete different things then regex are also allowed of course)

Kind regards

Saren

Let's write your first regex without the extra level of C escapes:

CRLocalizedString\(@\"[^)]+\"\)

You don't have to escape a " for a regex, so let's get rid of those extra backslashes:

CRLocalizedString\(@"[^)]+"\)

So, you want to match a quoted string using "[^)]+" . But that doesn't match every quoted string.

What is a quoted string? It's a " , followed by any number of string atoms, followed by another " . What is a string atom? It's any character except " or \\ , or a \\ followed by any character. So here's a regex for a quoted string:

"([^"\\]|\\.)*"

Sticking that back into your first regex, we get this:

CRLocalizedString\(@"([^"\\]|\\.)*"\)

Here's a link to a regex tester demonstrating that regex. Quoting it in an Objective-C string literal gives us this:

@"CRLocalizedString\\(@\"([^\"\\\\]|\\\\.)*\"\\)"

It is impossible to write a regex to match calls to CRLocalizedArgString in the general case, because such calls can take arbitrary expressions as arguments, and regexes cannot match arbitrary expressions (because they can contain arbitrary levels of nested parentheses, which regexes cannot match).

You could just hope that there are no parentheses in the argument list, and use this regex:

CRLocalizedArgString\(@"([^"\\]|\\.)*"[^)]*\)

Here's a link to a regex tester demonstrating that regex. Quoting it in an Objective-C string literal gives us this:

@"CRLocalizedArgString\\(@\"([^\"\\\\]|\\\\.)*\"[^)]*\\)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM