如何删除斜线不超过3个但保留较大链接的行?

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


一个会留下别的。

谢谢

最佳答案

搜索:(?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"

09-26 01:26