【问题标题】:Regex help for find and replace查找和替换的正则表达式帮助
【发布时间】:2017-08-01 16:22:31
【问题描述】:

我在记事本中有一个文件,其中包含以下文本:

*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}

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

【问题讨论】:

    标签: regex notepad++ data-munging


    【解决方案1】:

    使用记事本++:

    1. 按 Ctrl+H

    2. 在“查找内容”中,输入(.*)api(.*)\r

    3. 在“替换为”中,输入\1api\2{code}\r

    4. 勾选“搜索模式>正则表达式

    5. 点击“全部替换”

    【讨论】:

    • 你就是那个男人,它就像一个魅力!一旦允许,我会在 5 分钟内将其标记为已解决:-)
    【解决方案2】:
    • Ctrl+H
    • 查找内容:\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"
    

    【讨论】:

      猜你喜欢
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多