测试用例:
// 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], \1, \2)'
详细信息
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], \1, \2)'
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)
关于python - 用于查找文件内容中所有出现的 'NSLocalizedString' 的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54355207/