[英]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
一個匹配,有兩個捕獲組(鍵和注釋)。 Key 可能有一個值或為nil
。
我試過的: r'NSLocalizedString\\((.*)\\s*,\\s*(.*)\\)'
這適用於大多數情況,除了最后一個(內聯),因為它匹配最后一個逗號。
Regex101: https ://regex101.com/r/4OJgU2/6
您可以使用解決問題
r'(?s)NSLocalizedString\(\s*(@\"[^\"\\]*(?:\\.[^\"\\]*)*\"|\w+)\s*,\s*(@\"[^\"\\]*(?:\\.[^\"\\]*)*\"|\w+)\)'
和更換
r'NSLocalizedStringWithDefaultValue(@"elementID", nil, [NSBundle mainBundle], \1, \2)'
細節
NSLocalizedString\\(
- NSLocalizedString(
substring\\s*
- 0+ 個空格(@\\"[^\\"\\\\]*(?:\\\\.[^\\"\\\\]*)*\\"|\\w+)
- 第 1 組:
@\\"[^\\"\\\\]*(?:\\\\.[^\\"\\\\]*)*\\"
- @"
后跟 0+ 字符而不是"
和\\
跟 0+ 重復任何轉義char 后跟除"
和\\
之外的 0+ 個字符,然后是"
(它是 Obj-C 字符串文字匹配模式)|
- 或者\\w+
- 1+ 個字字符\\s*,\\s*
- ,
用 0+ 個空格括起來(@\\"[^\\"\\\\]*(?:\\\\.[^\\"\\\\]*)*\\"|\\w+)
- 第 2 組\\)
- a )
字符。請參閱Python 演示:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.