Regex help, replacing query with query

How to delete lines containing 3 or fewer stalks, but keep more links?

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

But nothing will leave.

thank

+4
source share
2 answers

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

Replace: ""

In demo, see how only lines with three or lesser lines are matched. These are the ones that were in nix.

Explain Regex

(?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"
+3
source

sed

You can use the following command sedto do this if your lines are in foo.txt:

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

The parameter -nhas no way out, but lines matching the pattern between /are printed anyway thanks to the command pat the end of the expression sed.

: 4 /, .

+1

Source: https://habr.com/ru/post/1543715/


All Articles