2014-06-08 78 views
4

如何刪除任何具有3個或更少斜槓但保留較大鏈接的行?正則表達式幫助,反查詢替換

A. http://two/three/four 
B. http://two/three 
C. http://two 

A會保持沒有別的。

感謝

+0

僅供參考添加演示和說明。如果您有任何問題,請告訴我。 :) – zx81

回答

3

搜索:(?m)^(?:[^/]*/){0,3}[^/]*$

替換:""

demo,看到3個或更少斜槓線怎麼只有匹配。這些是尼克斯的。

解釋的正則表達式

(?m)      # set flags for this block (with^and $ 
         # matching start and end of line) (case- 
         # sensitive) (with . not matching \n) 
         # (matching whitespace and # normally) 
^      # the beginning of a "line" 
(?:      # group, but do not capture (between 0 and 3 
         # times (matching the most amount 
         # possible)): 
    [^/]*     # any character except: '/' (0 or more 
         # times (matching the most amount 
         # possible)) 
/     # '/' 
){0,3}     # end of grouping 
[^/]*     # any character except: '/' (0 or more times 
         # (matching the most amount possible)) 
$      # before an optional \n, and the end of a 
         # "line" 
+0

這使我的頭受傷..但它的作品。謝謝。我還發現: '^((?!https://blah/.*/)。)* $' – lakecityransom

+0

@laccityransom不客氣。 :)嘿,因爲我看到你還沒有在SO上發佈upvote,請考慮投票你認爲有用的任何答案(感謝聽我的10秒SO tut :) – zx81

1

sed

您可以使用下面sed命令來做到這一點,假設你的線是foo.txt

sed -n '/\(.*\/\)\{4,\}/p' foo.txt 

-n選項是不輸出,但行數匹配之間的模式無論如何都要打印s,這要感謝sed表達式末尾的p命令。

該模式是:至少有4次出現/,每一個潛在地前面有任何其他字符串。

+0

實際上是一個編輯,但thx – lakecityransom

+0

Aww ...我不知道。我很高興你有一個可行的答案:)我希望我的對別人有用。 – Qeole