1. 程式人生 > >notepad++ 正則表達式(記錄)

notepad++ 正則表達式(記錄)

regress its express 個數 print 回車符 增加 server 一次

刪除操作
notepad++去掉行尾空格或逗號
查找目標:\s+$ (或,+$)
替換為空
Note: 以換行符結尾表示是$\r\n,而不是\r\n$

notepad++刪除文本文件裏面的空白行
查找目標:^[ \t]*\n或者:^\r\n
替換為空

notepad++去掉只有數字的行
查找目標:^[\d]+$\r\n
替換為空

notepad++去掉不是以某個數開頭的行
查找目標:^[^1].*\r\n
替換為空

notepad++去掉所有行中的<>(裏面不能嵌套<>)
查找目標:<[^>]*>
替換為空
input:
<code><span class="kwd">import</span><span class="pln"> </span><dfn><span class="typ">BaseHTTPServer</span></dfn><span class="pln">

</span><span class="kwd">import</span><span class="pln"> </span><dfn><span class="typ">SimpleHTTPServer</span></dfn><span class="pln">
</span><span class="kwd">import</span><span class="pln"> </span><dfn><span class="typ">CGIHttpServer</span></dfn></code>
output:
import BaseHTTPServer
import SimpleHTTPServer
import CGIHttpServer
notepad++去掉python代碼中的註釋
去掉‘‘‘ ‘‘‘之間的註釋
查找目標(regular expression)
^.*?‘‘‘\r\n.*?\r\n.*?‘‘‘\r\n
替換為空
去掉#註釋
查找目標(re)
^(.*?)#.*?\r\n
替換為
\1\r\n
再去掉空行
如果只去掉註釋行而不刪除代碼後面跟著的#註釋則查找目標為
^\s+#.*?\r\n

替換操作
notepad++替換所有行中的(Week 1) \n II. 為 ;
查找目標:\([^\.]*\.

替換為空
input:
I. Introduction機器學習綜述 (Week 1)

II. Linear Regression with One Variable單變量線性回歸 (Week 1)

III. Linear Algebra Review線性代數 (Week 1, Optional)
output:
I. Introduction機器學習綜述 ;Linear Regression with One Variable單變量線性回歸 ;Linear Algebra Review線性代數
Note:(和.都是特殊字符,要轉義;\(代表從字符(開始; [^\.]*代表非字符.的字符重復n次; \.表示到字符.為止
notepad++將[]及包含的字母替換成空
查找目標
[/]∗[[:alpha:]]∗
替換為空
input:
[cp] —你看我是歌手嗎? —不是。[/cp] [cp] 為什麽我的個子再也長不高了? 可能你得了恐高癥[/cp] [cp]
output:
—你看我是歌手嗎? —不是。 為什麽我的個子再也長不高了? 可能你得了恐高癥
notepad++替換括號中匹配的內容\1
1.在漢化的時候,是否經常碰到這樣的語句需要翻譯:
“Error adding the post!”;
“Error adding the comment!”;
“Error adding the user!”;
查找目標:
“Error adding ([^!|"|;]*)
替換成:
“在增加\1時發生錯誤
結果是:
“在增加the post時發生錯誤!”;
“在增加the comment時發生錯誤!”;
“在增加the user時發生錯誤!”;
Note:
1. ([^!|"|;]*) 的意思是 不等於 ! 和 ” 和 ; 中的任何一個,意思就是這3個字符之外的所有字符將被選中(替換區域);
2. 正則表達式中\1表示第一個括號裏面匹配內容。
正則表達式將倒數第一個\t替換為=>
biscuits milk
yoghurt milk
tomato souce pasta
tomato souce milk
water pasta milk
查找目標:(RE)
\t(\w+?)\r\n
替換為:
=>\1\r\n
結果:
biscuits=>milk
yoghurt=>milk
tomato souce=>pasta
tomato souce=>milk
water pasta=>milk
正則表達式將數字開頭替換為數字.開頭
1.os.sep 可以取代
2.os.name
3os
查找目標:
^(\d+)\.*
替換為:
\1.
結果:
1.os.sep 可以取代
2.os.name
3.os
Notepad++中在小數和字母間加上*號
查找目標
(\d\.\d+)
替換為
\1\*
input:
0.95c == 0.9b + 0.475a
c == 0.9b + 0.475a
0.85a == c + 0.15b
c == b + 0.575a
output:
0.95*c == 0.9*b + 0.475*a
c == 0.9*b + 0.475*a
0.85*a == c + 0.15*b
c == b + 0.575*a
Notepad++中在字符串上加上引號
查找目標
(\w+)
替換為
‘\1‘
input:
c, i, nd, o, p, u
output:
‘c‘, ‘i‘, ‘nd‘, ‘o‘, ‘p‘, ‘u‘
Notepad++將每行賦值語句修改成判斷語句
查找目標
^(.*)$
替換為
if \1 :\n\tprint′True′
input:
0.95*c == 0.9*b + 0.475*a
c == 0.9*b + 0.475*a
0.85*a == c + 0.15*b
c == b + 0.575*a
output:
if 0.95*c == 0.9*b + 0.475*a :
print(‘True‘)
if c == 0.9*b + 0.475*a :
print(‘True‘)
if 0.85*a == c + 0.15*b :
print(‘True‘)
if c == b + 0.575*a :
print(‘True‘)

查找操作
notepad++查找括號中匹配的內容\1
查找出(0 0 1)(0 1 1)T, x③=(-1 0 -1)T, x④=(-1 -1 -1)中小括號內的內容
查找目標:
.*?((−∗\d\s∗)+).*?
替換成:
\1
結果是:
(0 0 1)(0 1 1)(-1 0 -1)(-1 -1 -1)
Note: 這個查找效果不是很好,達不到用Python編寫re.findall()的效果。
notepad++查找括號()中的內容\1
查找出
ω1:{(1 0)T, (2 0) T, (1 1) T}
ω2:{(-1 0)T, (0 1) T, (-1 1) T}
ω3:{(-1 -1)T, (0 -1) T, (0 -2) T}
中小括號內的內容
查找目標:
.*?(\-∗\d\s\-∗\d).*?
替換成:
\1
結果是:
(1 0)(2 0)(1 1) T}
(-1 0)(0 1)(-1 1) T}
(-1 -1)(0 -1)(0 -2) T}

\t 制表符(tab)
\r 回車符
\n 新行
. 匹配任意字符.
| 匹配表達式左邊和右邊的字符.
例如, "ab|bc" 匹配 "ab" 或者 "bc".
[] 匹配列表之中的任何單個字符.
例如, "[ab]" 匹配 "a" 或者 "b". "[0-9]" 匹配任意數字.
[^] 匹配列表之外的任何單個字符.
例如, "[^ab]" 匹配 "a" 和 "b" 以外的字符. "[^0-9]" 匹配任意非數字字符.
* 其左邊的字符被匹配任意次(0次,或者多次).
例如 "be*" 匹配 "b", "be" 或者 "bee".
+ 其左邊的字符被匹配至少一次(1次,或者多次).
例如 "be+" 匹配 "be" 或者 "bee" 但是不匹配 "b".
? 其左邊的字符被匹配0次或者1次.
例如 "be?" 匹配 "b" 或者 "be" 但是不匹配 "bee".
^ 其右邊的表達式被匹配在一行的開始.
例如 "^A" 僅僅匹配以 "A" 開頭的行.
$ 其左邊的表達式被匹配在一行的結尾.
例如 "e$" 僅僅匹配以 "e" 結尾的行.
() 影響表達式匹配的順序,並且用作表達式的分組標記.
\ 轉義字符. 如果你要使用 "" 本身, 則應該使用 "\".

notepad++ 正則表達式(記錄)