2014-05-09 32 views
0

我需要一個正則表達式來查找文檔中的某些文本,並更改​​並粘貼到新行。崇高的文本查找,複製,更改並粘貼在新行

logging.info("hello i am here") 

需要找到logging.info的所有發生改變

print("hello i am here") 

最後看起來像這樣

logging.info("hello i am here") 
print("hello i am here") 

是他們的任何正則表達式,我可以做什麼或我需要做的手動。

+0

老闆,我需要將其粘貼到新行沒有查找和替換。我想你明白了我的意思。 –

+0

好的......現在我明白你的意思了。 「需要查找所有logging.info出現並更改爲......」的部分將我拋棄了。你有沒有嘗試過任何東西? 'logging.info'總是獨自在一條線上?你想保留縮進嗎? – Jerry

+0

是的,現在讓我。是的縮進是必須的。因爲我使用python代碼。 –

回答

6

我相信這樣的正則表達式應該工作:

^(\h*)logging\.info\(([^)]*)\) 

替換:

$0\n$1print($2) 

regex101 demo

說明:

^     # Beginning of line 
(\h*)    # Get any spaces/tabs before the line and store in $1 
logging\.info\( # Match 'logging.info(' 
([^)]*)   # Get everything between parens 
\)    # Match closing paren 

注意,以上正則表達式假定logging.info函數中沒有其他的參數。

的替代手段:

$0    # Whole match 
\n    # Newline 
$1    # Place the indentation 
print(   # 'print(' 
$2    # The part within the `logging.info` function 
)     # Closing paren 
+0

正則表達式工作正常,但不在崇高。 –

+0

@ user2190040你檢查了正則表達式模式嗎?它是帶''*'的頂部最左邊的按鈕。以下是我的ST3版本:[之前](http://i.stack.imgur.com/32N8F.png)和[之後](http://i.stack.imgur.com/1sHG1.png) – Jerry

+0

yes工作^(\ h *)logging \ .info \(([^)] *)\)以前我正在嘗試^([] *)logging \ .info \(([^)] *)\) –