/ / Regex-Hilfe, Anti-Abfrage-Ersatz - Regex

Regex Help, Anti-Abfrage-Ersatz - Regex

Wie entferne ich Zeilen, die 3 oder weniger Schrägstriche haben, aber größere Links behalten?

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

Ein würde bleiben nichts anderes würde.

Vielen Dank

Antworten:

3 für die Antwort № 1

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

Ersetzen: ""

Auf der Demo, sehen Sie, wie nur die Zeilen mit 3 oder weniger Schrägstrichen übereinstimmen. Dies sind die zu nix.

Erklären 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"

1 für die Antwort № 2

Sie können Folgendes verwenden sed Befehl, dies zu tun, vorausgesetzt, Ihre Linien sind in foo.txt:

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

Das -n Option ist für keine Ausgabe, sondern Linien, die das Muster zwischen den /s sind trotzdem gedruckt dank dem p Befehl am Ende der sed Ausdruck.

Das Muster ist: mindestens 4 Vorkommen von /, jedem, dem möglicherweise eine andere Zeichenfolge vorausgeht.