Python 正则表达式以查找所有出现的';NSLocalizedString';文件中的内容

Python 正则表达式以查找所有出现的';NSLocalizedString';文件中的内容,python,regex,string-parsing,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 (wit

测试用例:

// 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*(.*)”

这适用于大多数情况,但最后一个(内联)除外,因为它在最后一个逗号处匹配


Regex101:

您可以使用

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组
  • \)
    -a
    字符
见:

输出:

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

我认为不可能捕获其中的参数,但您可能会使用什么来匹配所有子字符串将看起来像@WiktorStribiżew这是一个很好的开始,谢谢!为什么不可能捕捉到里面的元素呢?因为它们都是一个整体的一部分。您可以分两步完成:1)-将它们捕获到组1中,2)使用@WiktorStribiżew重新解析(重新匹配),这可以避免吗?或者有其他小的捕获组在里面,匹配的关键和评论。。。只有两个捕获组并不是一个困难的要求。在Python中,您甚至可以使用PyPi regex模块来访问一个组中的所有捕获。你能使用PyPi正则表达式模块吗?
----------------------------------
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)