python - 用于查找文件内容中所有出现的 'NSLocalizedString' 的正则表达式

标签 python regex string-parsing nslocalizedstring

测试用例:

// 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/

相关文章:

regex - 其他记录之间的字符串列表加载错误Hive

javascript - 使用单个正则表达式仅提取函数名称(字符串值)

c# - 删除文本文件中的空行

java - 使用 Java 8 java.time api 解析 ISO 时间戳(仅限标准版)

python - f2py:预分配数组作为 Fortran 子例程的输入

python - 使用 numpy ndarray 计算平均值

python - 使用 Python Pandas 按不同字符切片字符串

c++ - 如何正确解释数字(十六进制、十进制、十进制)

python - 我需要以相同的随机方式随机洗牌两个 pandas DataFrame 的行

python - Django 语法高亮导致字符转义问题