Regular expression

From LemonWiki共筆
Jump to navigation Jump to search

case

將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號)

[email protected]
[email protected]
[email protected]

改成
[email protected], [email protected], [email protected]

方案1: EmEditor

使用EmEditor軟體

  1. Menu: Search -> Replace
  2. click "Use Regular Expression"
    1. Find: \n
    2. Replace with: ,
  3. click "Replace all"

方案2: Notepad++

使用Notepad++軟體

  1. 選單: 尋找 -> 取代
  2. 搜尋模式: 勾選「增強模式」 (不是勾選「用類型表式」)
    1. 尋找目標: \r\n
    2. 取代成: ,
  3. 勾選全部取代

相關資料: How To Replace Line Ends, thus changing the line layout last visited: 2010-01-27

方案3: Microsoft Word

使用Microsoft Word 2002軟體

  1. 選單: 編輯 -> 取代
  2. 勾選增強模式
    1. 尋找目標: ^p (段落標記)
    2. 取代為: ,
  3. 勾選全部取代


Find IP address

使用Notepad++軟體 v.5.9.5

  1. 選單: 尋找 -> 取代
  2. 搜尋模式: 勾選「用類型表式」
    1. 尋找目標: \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++軟體

  1. 選單: 尋找 -> 取代
  2. 搜尋模式: 勾選「增強模式」
    1. 尋找目標: \n\n (註: 2個LF )
    2. 取代成: \r\n (註: CR與LF )

用記事本打開純文字檔時,就不會看到黑色方塊


將陣列的每項元素,都加上引號框起來

Elmo, Emie, Granny Bird, Herry Monster, 喀喀獸
修改成
'Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸'

使用 PHP

$users = array('Elmo', 'Emie', 'Granny Bird', 'Herry Monster', '喀喀獸');
//引號相隔每個元素
$result = implode(",\n", preg_replace('/^(.*?)$/', "'$1'", $users));
//如果要改成用雙引號相隔每個元素
//$result = implode(",\n", preg_replace('/^(.*?)$/', "\"$1\"", $users));
echo $result;

Thanks, Joshua! More on PHP - Wrap Implode Array Elements in Quotes » Me Like Dev


取代非英文的文字

適用: Google Drive 的 RegExReplace 函數

[^\x00-\x80]+

適用: Total commander 的 Multi-Rename tool[1]

[^\u0000-\u0080]+

參考資料: javascript - Regular expression to match non-english characters? - Stack Overflow

syntax

  • 換行符號: \r\n (適用: Notepad++選項: 增強模式 & 用類型表式)
  • tab鍵的固定空白分隔: \t (適用: Notepad++選項: 增強模式)
  • 數字: \d (適用: Notepad++選項: 用類型表式。Icon_exclaim.gif 不適用: Notepad++選項: 增強模式)

further reading

  1. 取代非英文的文字,但是不包含 . 符號: [^\u0000-\u0080|.]+

替代方案

  • 將資料以 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 Icon_exclaim.gif
  • Copy to MS Excel 2002 from Google Docs: ok