正则表达式帮助查找和替换

时间:2017-08-01 16:22:31

标签: regex notepad++ data-munging

我在记事本中有一个文件,例如:

*Given* I get an user ID from XXX
*And* I set header "Authorization" with value "invalid_token"
*When* I send a POST request to api/endpoint/"documentlibraryID"/"identity_id"/root/"new_name"
*Then* the response code should be 401
*And* the response should contain "Invalid authorization token"

*Given* I get an user ID from XXX
*And* I set header "Authorization" with value "YYY"
*When* I send a POST request to api/endpoint/"documentlibraryID"/"identity_id"/root/"new_name"
*Then* the response code should be 200
*And* the response should contain "new_name"

*Given* I get an user ID from "XXX"
*And* I set header "Authorization" with value "YYY"
*When* I send a POST request to api/endpoint/"documentlibraryID"/"identity_id"/root/"folder_name"?automaticRename=true
*Then* the response code should be 200
*And* the response should contain "folder_name 1"

我需要做什么:在每个单词 api 之前,我需要插入{code:none}并在行尾插入{code} 。例如:

api/endpoint/"documentlibraryID"/"identity_id"/root/"folder_name"?automaticRename=true

将是:

{code:none}api/endpoint/"documentlibraryID"/"identity_id"/root/"folder_name"?automaticRename=true{code}

第一部分很简单,我只需在Notepad ++中替换 {code:none} api api 。最后一部分是我的问题。并非所有行都以相同的文本结尾...所以我需要找到一个正则表达式,在每行的末尾插入{code},它会在某处找到单词 api ,或者其他一些方法......不确定这是否清楚,我可以尝试更好地解释,谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

使用Notepad ++:

  1. 按Ctrl + H

  2. 在"找到",输入(.*)api(.*)\r

  3. 在"替换为",键入\1api\2{code}\r

  4. 检查"搜索模式>正则表达式

  5. 点击"全部替换"

答案 1 :(得分:1)

  • 控制 + ħ
  • 找到:\bapi\b.*$
  • 替换为:{code:none}$0{code}
  • 全部替换

<强>解释

\bapi\b : api not preceeded or followed by word character
.*      : 0 or more any character
$       : end of line
  • 请勿检查. matches newline

<强>替换

{code:none} : literally
$0          : the whole match
{code}      : literally

给定示例的结果:

*Given* I get an user ID from XXX
*And* I set header "Authorization" with value "invalid_token"
*When* I send a POST request to {code:none}api/endpoint/"documentlibraryID"/"identity_id"/root/"new_name"{code}
*Then* the response code should be 401
*And* the response should contain "Invalid authorization token"

*Given* I get an user ID from XXX
*And* I set header "Authorization" with value "YYY"
*When* I send a POST request to {code:none}api/endpoint/"documentlibraryID"/"identity_id"/root/"new_name"{code}
*Then* the response code should be 200
*And* the response should contain "new_name"

*Given* I get an user ID from "XXX"
*And* I set header "Authorization" with value "YYY"
*When* I send a POST request to {code:none}api/endpoint/"documentlibraryID"/"identity_id"/root/"folder_name"?automaticRename=true{code}
*Then* the response code should be 200
*And* the response should contain "folder_name 1"