Regular expression: Difference between revisions
| Line 303: | Line 303: | ||
=== 將每行文字的結尾處,加入空一格 (半形空白) === | === 將每行文字的結尾處,加入空一格 (半形空白) === | ||
適用軟體: Sublime Text, EmEditor | |||
# Menu: Search -> Replace | # Menu: Search -> Replace | ||
# click "Use Regular Expression" | # click "Use Regular Expression" | ||
| Line 309: | Line 309: | ||
## Replace with: {{kbd | key = <nowiki>_\n</nowiki>}}(符號 {{kbd | key = <nowiki>\n</nowiki>}} 前面的 _ 自行替換成半形空白) | ## Replace with: {{kbd | key = <nowiki>_\n</nowiki>}}(符號 {{kbd | key = <nowiki>\n</nowiki>}} 前面的 _ 自行替換成半形空白) | ||
# click "Replace all" | # click "Replace all" | ||
{{exclaim}} 需要檢查最後一行是否是空白行,如果不是空白行,不會套用到該取代規則 | |||
=== 將每行文字內夾雜的空白,改成 Tab 符號 === | === 將每行文字內夾雜的空白,改成 Tab 符號 === | ||
Revision as of 16:58, 15 June 2015
正規表示法 (Regular Expression):處理文字檔時,搜尋或取代符合特定規則的字串,以文字檔每行的字串為單位處理。[1]
有問題嗎?可以利用提供解說的 線上工具,嘗試自己除錯。 也可以到看板 RegExp 文章列表 - 批踢踢實業坊或其他問答服務詢問。
快速查表
說明: sample 藍色網底處代表符合規則的文字
| 文字規則 | sample | 對立的文字規則 | sample |
|---|---|---|---|
| 任意一個文字(包含空白,但不包含換行符號) . |
What Does the Fox Say? 12 狐狸怎叫 34 | ||
| 任意文字(包含空白),出現1次或0次 .? = .{0,1} |
What Does the Fox Say? 12 狐狸怎叫 34 | ||
| 任意次的多個文字(包含空白) .* = .{0,} |
What Does the Fox Say? 12 狐狸怎叫 34 | ||
| 任意次的文字(包含空白),至少出現1次 .+ = .{1,} |
What Does the Fox Say? 12 狐狸怎叫 34 | ||
| 任意次的空白 (至少出現1次的空白) \s+ |
What Does the Fox Say? 12 狐狸怎叫 34 | 任意多個文字(不包含空白) [^\s]+ = [^\s]{1,} = [\S]+ |
What Does the Fox Say? 12 狐狸怎叫 34 |
| 任意次的 ASCII character(包含英文、數字和空白) demo[2] [\x00-\x80]+ |
What Does the Fox Say? 12 狐狸怎叫 34 | 非 ASCII,即中文出現任意次 [^\x00-\x80]+ |
What Does the Fox Say? 12 狐狸怎叫 34 |
| 任意次的英文、數字和底線符號( _ )文字(不包含空白) [\w]+ = [a-zA-Z0-9_]+ |
What Does the Fox Say? 12 狐狸怎叫 34 | ||
| 任意次的數字(不包含空白) [\d]+ = [0-9]+ |
What Does the Fox Say? 12 狐狸怎叫 34 | 不包含數字的任意次文字 [^\d]+ |
What Does the Fox Say? 12 狐狸怎叫 34 |
| 以「狐狸」開頭的行 ^狐狸.*$[3] |
狐狸怎叫 34 What Does the Fox Say? |
不以「狐狸」開頭的行 ^(?!狐狸).*$[4] |
狐狸怎叫 34 What Does the Fox Say? |
| 包含「狐狸」的行 ^.*狐狸.*$ |
What Does the Fox Say? 12 狐狸怎叫 34 |
不包「狐狸」的行 ^((?!狐狸).)*$ |
What Does the Fox Say? 12 狐狸怎叫 34 |
| 布林邏輯 AND: 包含「狐狸」和「叫」的行 (demo)[5] (?=.*狐狸)(?=.*叫).* |
What Does the Fox Say? 12 狐狸怎叫 34 |
||
| 布林邏輯 OR: 包含「狐狸」或「叫」的行 (demo) .*(狐狸|叫).* |
What Does the Fox Say? 12 狐狸怎叫 34 |
||
| 布林邏輯 NOT: 不包含「狐狸」,但包含「柴犬」的行 (demo)[6] ^((?!狐狸).)*(柴犬).*$ = ^(柴犬).*((?!狐狸).)*$ = (柴犬).*((?!狐狸).)* |
What Does the Fox Say? 12 狐狸怎叫 34 |
Regular expression online tools
- Online regex tester and debugger: JavaScript, Python, PHP, and PCRE (example)
有提供語法解說 - Rubular: a Ruby regular expression editor and tester (example)
- RegExr: Learn, Build, & Test RegEx (example).
有提供語法解說. 教學: RegExr: 功能強大的正規式撰寫協助工具 - PHP Live Regex [Last visited: 2014-11-25]
- HiFi Regex Tester - Live JavaScript Regular Expression Tester for Javascript [Last visited: 2014-12-23]
cases
將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號)
原 [email protected] [email protected] [email protected] 改成 [email protected], [email protected], [email protected]
方案1: Sublime Text, EmEditor
語法適用 Sublime Text, EmEditor軟體 (以下為 EmEditor 的操作說明)
- Menu: Search -> Replace
- click "Use Regular Expression"
- Find: \n
- Replace with: ,
- click "Replace all"
方案2: Notepad++
使用Notepad++軟體
- 選單: 尋找 -> 取代
- 搜尋模式: 勾選「增強模式」 (不是勾選「用類型表式」)
- 尋找目標: \r\n
- 取代成: ,
- 勾選全部取代
相關資料: How To Replace Line Ends, thus changing the line layout last visited: 2010-01-27
方案3: Microsoft Word
使用Microsoft Word 2002軟體
- 選單: 編輯 -> 取代
- 勾選增強模式
- 尋找目標: ^p (段落標記)
- 取代為: ,
- 勾選全部取代
方案4: Sed command for linux
sed 's/要被取代的字串/新的字串/g' old.filename > new.filename[7]
(1)要被取代的字串: :a;N;$!ba;s/\n (2)新的字串: ;
sed ':a;N;$!ba;s/\n/; /g' old.filename > new.filename [8]
Find IP address
使用Notepad++軟體 v.5.9.5
- 選單: 尋找 -> 取代
- 搜尋模式: 勾選「用類型表式」
- 尋找目標: \d\d?\d?\.\d\d?\d?\.\d\d?\d?\.\d\d?\d?
note: not support {n} syntax
參考資料: SourceForge.net: Notepad++: Regular expression for IP addresses
移除記事本純文字檔的黑色方塊(UNIX系統的換行符號 LF )
使用notepad++軟體
- 選單: 尋找 -> 取代
- 搜尋模式: 勾選「增強模式」
- 尋找目標: \n\n (註: 2個LF )
- 取代成: \r\n (註: CR與LF )
用記事本打開純文字檔時,就不會看到黑色方塊
將陣列的每項元素,都加上引號框起來
Elmo, Emie, Granny Bird, Herry Monster, 喀喀獸 修改成 'Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸'
方法1: 使用 PHP
$users = array('Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸');
//「單引號」相隔每個元素
$result = implode(",", preg_replace('/^(.*?)$/', "'$1'", $users));
//「雙引號」相隔每個元素
//$result = implode(",", preg_replace('/^(.*?)$/', "\"$1\"", $users));
echo $result;
Thanks, Joshua! More on PHP - Wrap Implode Array Elements in Quotes » Me Like Dev
方法2: 使用 Sublime Text 或 EmEditor
- Find: ([^\s|,]+)
- 分隔符號
- 「單引號」相隔每個元素 Replace with: '\1'
- 「雙引號」相隔每個元素 Replace with: "\1"
方法3: 使用 Notepad++。啟用搜尋模式的「用類型表式」
- Find: ([^\s|,]+)
- 分隔符號
- 「單引號」相隔每個元素 Replace with: '$1'
- 「雙引號」相隔每個元素 Replace with: "$1"
取代非英文的文字
適用: Google Drive 的 RegExReplace 函數、Notepad++的搜尋
[^\x00-\x80]+
適用: Total commander 的 Multi-Rename tool[9]
[^\u0000-\u0080]+
參考資料: javascript - Regular expression to match non-english characters? - Stack Overflow
將每行文字的行頭加上逗號符號
使用notepad++軟體
- 選單: 尋找 -> 取代
- 搜尋模式: 勾選「用類型表示」
- 尋找目標: (.*) 或者是 ^(.*)$
- 取代成: ,\1 或者是 ,$1。
將行內一個或多個空白,取代成 Tab鍵
將原本空白間隔的欄位值,取代成 Tab鍵間隔的欄位值。輸出結果可以方便貼到 MS Excel 或 Google spreadsheet。
欄位1 多個空白 欄位2 多個空白 欄位3 -> 欄位1 Tab鍵 欄位2 Tab鍵 欄位3 (本行文字為方便說明,用空白間隔,實際上是沒有空白的)
說明: \S 代表非空白字元, \r\n 代表換行符號。[^\S\r\n] 則代表不是非空白字元、也不是換行符號。換句話說尋找空白,但不包含換行符號。
- Find:
([^\S\r\n]+) - Replace with:
\t
知道前面跟後面的文字,但是中間文字忘記了
使用notepad++軟體
- 選單: 尋找 -> 取代
- 搜尋模式: 勾選「用類型表示」
- 尋找目標: a(.*)le 就可以找到(1)apple (2)apps lesson ... 等a開頭、le結尾的文字,中間可夾雜空白。
中文字串搜尋,建議將文件的編碼改成 UTF-8 編碼
- 尋找目標: a(.*)le 就可以找到(1)apple (2)apps lesson ... 等a開頭、le結尾的文字,中間可夾雜空白。
移除空白行
移除一行空白或多行空白(含空白字元)
- 尋找: ^[\s\t]*$\n --> 取代為: 空白 (適用 Sublime Text 與 EmEditor 軟體,
不適用 Notepad++ 軟體)[10] - Notepad++ 軟體選單: 編輯 -> 行列 -> 移除空行(含空白字元)[11]
移除一行空白或多行空白
- 尋找: ^$\n --> 取代為: 空白 (適用 Sublime Text 與 EmEditor 軟體,
不適用 Notepad++ 軟體) - 尋找: \r\n[\r\n]* 或 \r\n[\r\n]+ --> 取代為: \r\n (適用 Notepad++ 軟體,需勾選「用類型表式)
- 尋找: \n(\n)+ --> 取代為: \n(適用 Sublime Text 軟體,需 勾選「regular expression」)
移除一行空白
- 尋找: \n\n --> 取代為: \n (適用 Sublime Text 與 EmEditor 軟體,需勾選「使用規則運算式」)
尋找非空白的文字
- 尋找: [^\s]+ online demo
將特定符號相隔的文字,改成逐行顯示
例子:
# (原) 頓號(、)符號相隔的文字 尼歐、莫斐斯、崔妮蒂、史密斯、祭師 # (後) 改成逐行顯示 尼歐 莫斐斯 崔妮蒂 史密斯 祭師
使用 Sublime Text 或 EmEditor
- Find: ([^、]+)([、]{1})
- Replace with: \1\n
語法說明
- [^、] : 符合任意字,但不是頓號(、)的文字
- [^、]+ : 一次以上不是頓號(、)的文字
- ([^、]+) : 符合「一次以上不是頓號(、)的文字」規則的文字
- [、]: 出現頓號(、)任意次的文字
- [、]{1} : 出現頓號(、)一次的文字
- ([、]{1}) : 符合「出現頓號(、)一次的文字」規則的文字
將每行文字的結尾處,加入空一格 (半形空白)
適用軟體: Sublime Text, EmEditor
- Menu: Search -> Replace
- click "Use Regular Expression"
- Find: \n
- Replace with: _\n(符號 \n 前面的 _ 自行替換成半形空白)
- click "Replace all"
需要檢查最後一行是否是空白行,如果不是空白行,不會套用到該取代規則
將每行文字內夾雜的空白,改成 Tab 符號
# before aaa bbb ccc # after aaa\tbbb\tccc
使用 Sublime Text 軟體 (參考資料[12] [13])
- Menu: Search -> Replace
- click "Use Regular Expression"
- Find: ([^\S\n]+) 或 _{1,} ( _ 自行替換成半形空白)
- Replace with: \t
- click "Replace all"
Search unmatched string
case: find un-commented console.log
original format: some lines contains un-commented Javascript debug information
console.log("un-commented debug information");
//console.log("commented debug information");
Search pattern: find not started with the / symbol before the string "console.log"
[^/](console\.log)
batch action
RegReplace 執行多個取代命令 "Simple find and replace sequencer plugin for Sublime Text" Quoted from official webpage. [Last visited: 2014-10-25]
syntax
- 換行符號: \r\n (適用: Notepad++選項: 增強模式 & 用類型表式)
- tab鍵的固定空白分隔: \t (適用: Notepad++選項: 增強模式)
- 數字: \d (適用: Notepad++選項: 用類型表式。
不適用: Notepad++選項: 增強模式) - \S 非空白的文字: 不會含括半形空白與全行空白
further reading
Regular Expression Library 網友提供的 pattern 範例- SourceForge.net: Searching And Replacing - notepad-plus, SourceForge.net: Regular Expressions - notepad-plus
- regex - text extraction with sublime text - Stack Overflow [Last visited: 2014-09-26]
unicode
- Regex Tutorial - Unicode Characters and Properties [Last visited: 2014-04-02]
- PHP: Unicode character properties - Manual [Last visited: 2014-04-02]
references
- ↑ 鳥哥的 Linux 私房菜 -- 正規表示法 (regular expression, RE) 與文件格式化處理
- ↑ understand
- ↑ Regex Examples: Matching Whole Lines of Text That Satisfy Certain Requirements
- ↑ regex - Regular expression to match text that *doesn't* contain a word? - Stack Overflow
- ↑ regex - Regular Expressions: Is there an AND operator? - Stack Overflow
- ↑ regex - Regular expression for a string containing one word but not another - Stack Overflow
- ↑ 鳥哥的 Linux 私房菜 -- 正規表示法 (regular expression, RE) 與文件格式化處理
- ↑ 參考 unix - sed: How can I replace a newline?
- ↑ 取代非英文的文字,但是不包含 . 符號: [^\u0000-\u0080|.]+
- ↑ Regex: delete multiple blank lines
- ↑ regex - Removing empty lines in Notepad++ - Stack Overflow
- ↑ Quickly replace multiple space characters with a tab character - TechRepublic
- ↑ regex - Match whitespace but not newlines (Perl) - Stack Overflow
替代方案
- 將資料以 Tab來隔開,貼到Google Drive的Spreadsheet或MS Excel,會自動儲存到不同欄位。所以將需要處理的原始資料中,需要擷取的資料的前後,使用Tab來隔開,複製後貼到於Google Drive的Spreadsheet或MS Excel,就會自動儲存到不同欄位,方便做進一步處理。
Copy multiple rows & paste
- Copy to dreamweaver from MS Excel 2002: ok
- Copy to dreamweaver from Google Docs: not ok

- Copy to MS Excel 2002 from Google Docs: ok