用于查找文件内容中所有出现的 'NSLocalizedString' 的正则表达式
Regex expression to find all occurences of 'NSLocalizedString' in file contents
测试用例:
// With text and comment
NSLocalizedString(@"Example Text", @"Example Comment");
// With text and no comment
NSLocalizedString(@"Example, text", nil)
// With text and comment with paranthesis
NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")
// With property and no comment
NSLocalizedString(test, nil)
// With property and comment
NSLocalizedString(test, @"Example comment")
// Inline
NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)
我要找的是:每次 NSLocalizedString
出现一个匹配项,有两个捕获组(键和注释)。键可能有一个值或者是 nil
.
我尝试过的:r'NSLocalizedString\((.*)\s*,\s*(.*)\)'
这适用于大多数情况,除了最后一个(内联),因为它匹配最后一个逗号。
正则表达式 101:https://regex101.com/r/4OJgU2/6
您可以使用
解决问题
r'(?s)NSLocalizedString\(\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\s*,\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\)'
和替换
r'NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], , )'
详情
NSLocalizedString\(
- NSLocalizedString(
子字符串
\s*
- 0+ 个空格
(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)
- 第 1 组:
@\"[^\"\]*(?:\.[^\"\]*)*\"
- @"
后跟 "
以外的 0+ 个字符,\
后跟任何转义字符的 0+ 次重复,后跟 0+ 个字符以外的字符"
和 \
然后是 "
(它是 Obj-C 字符串文字匹配模式)
|
- 或
\w+
- 1+ 个单词字符
\s*,\s*
- ,
包含 0+ 个空格
(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)
- 第 2 组
\)
- 一个 )
字符。
参见 Python demo:
import re
strs = ['NSLocalizedString(@"Example Text", @"Example Comment");', 'NSLocalizedString(@"Example, text", nil)', 'NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")', 'NSLocalizedString(test, nil)', 'NSLocalizedString(test, @"Example comment")', 'NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)']
pat = re.compile(r'NSLocalizedString\(\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\s*,\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\)', re.DOTALL)
repl = r'NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], , )'
for s in strs:
print('----------------------------------\n{}\nVVVVVVVVVVVVVVVVVVVV'.format(s))
res = pat.sub(repl, s)
print(res)
输出:
----------------------------------
NSLocalizedString(@"Example Text", @"Example Comment");
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example Text", @"Example Comment");
----------------------------------
NSLocalizedString(@"Example, text", nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example, text", nil)
----------------------------------
NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example text", @"Example (with paranthesis) comment")
----------------------------------
NSLocalizedString(test, nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], test, nil)
----------------------------------
NSLocalizedString(test, @"Example comment")
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], test, @"Example comment")
----------------------------------
NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Error", nil) NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Change settings", @"Option to change HTTP Post settings") NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Cancel", nil)
测试用例:
// With text and comment
NSLocalizedString(@"Example Text", @"Example Comment");
// With text and no comment
NSLocalizedString(@"Example, text", nil)
// With text and comment with paranthesis
NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")
// With property and no comment
NSLocalizedString(test, nil)
// With property and comment
NSLocalizedString(test, @"Example comment")
// Inline
NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)
我要找的是:每次 NSLocalizedString
出现一个匹配项,有两个捕获组(键和注释)。键可能有一个值或者是 nil
.
我尝试过的:r'NSLocalizedString\((.*)\s*,\s*(.*)\)'
这适用于大多数情况,除了最后一个(内联),因为它匹配最后一个逗号。
正则表达式 101:https://regex101.com/r/4OJgU2/6
您可以使用
解决问题r'(?s)NSLocalizedString\(\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\s*,\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\)'
和替换
r'NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], , )'
详情
NSLocalizedString\(
-NSLocalizedString(
子字符串\s*
- 0+ 个空格(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)
- 第 1 组:@\"[^\"\]*(?:\.[^\"\]*)*\"
-@"
后跟"
以外的 0+ 个字符,\
后跟任何转义字符的 0+ 次重复,后跟 0+ 个字符以外的字符"
和\
然后是"
(它是 Obj-C 字符串文字匹配模式)|
- 或\w+
- 1+ 个单词字符
\s*,\s*
-,
包含 0+ 个空格(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)
- 第 2 组\)
- 一个)
字符。
参见 Python demo:
import re
strs = ['NSLocalizedString(@"Example Text", @"Example Comment");', 'NSLocalizedString(@"Example, text", nil)', 'NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")', 'NSLocalizedString(test, nil)', 'NSLocalizedString(test, @"Example comment")', 'NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)']
pat = re.compile(r'NSLocalizedString\(\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\s*,\s*(@\"[^\"\]*(?:\.[^\"\]*)*\"|\w+)\)', re.DOTALL)
repl = r'NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], , )'
for s in strs:
print('----------------------------------\n{}\nVVVVVVVVVVVVVVVVVVVV'.format(s))
res = pat.sub(repl, s)
print(res)
输出:
----------------------------------
NSLocalizedString(@"Example Text", @"Example Comment");
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example Text", @"Example Comment");
----------------------------------
NSLocalizedString(@"Example, text", nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example, text", nil)
----------------------------------
NSLocalizedString(@"Example text", @"Example (with paranthesis) comment")
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Example text", @"Example (with paranthesis) comment")
----------------------------------
NSLocalizedString(test, nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], test, nil)
----------------------------------
NSLocalizedString(test, @"Example comment")
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], test, @"Example comment")
----------------------------------
NSLocalizedString(@"Error", nil) NSLocalizedString(@"Change settings", @"Option to change HTTP Post settings") NSLocalizedString(@"Cancel", nil)
VVVVVVVVVVVVVVVVVVVV
NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Error", nil) NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Change settings", @"Option to change HTTP Post settings") NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], @"Cancel", nil)