Ich brauche Hilfe bei der Deklaration einer Regex. Meine Eingaben sind wie folgt:
this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>
Die erforderliche Ausgabe ist:
this is a paragraph with in between and then there are cases ... where the number ranges from 1-100.
and there are many other lines in the txt files
with such tags
Ich habe das probiert:
#!/usr/bin/python
import os, sys, re, glob
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
for line in reader:
line2 = line.replace('<[1> ', '')
line = line2.replace('</[1> ', '')
line2 = line.replace('<[1>', '')
line = line2.replace('</[1>', '')
print line
Ich habe es auch ausprobiert (aber es scheint, als würde ich die falsche Regex-Syntax verwenden):
line2 = line.replace('<[*> ', '')
line = line2.replace('</[*> ', '')
line2 = line.replace('<[*>', '')
line = line2.replace('</[*>', '')
Ich möchte die replace
nicht hart von 1 bis 99 kodieren. . .
Dieses getestete Snippet sollte es tun:
import re
line = re.sub(r"</?\[\d+>", "", line)
Edit: Hier ist eine kommentierte Version, die erklärt, wie es funktioniert:
line = re.sub(r"""
(?x) # Use free-spacing mode.
< # Match a literal '<'
/? # Optionally match a '/'
\[ # Match a literal '['
\d+ # Match one or more digits
> # Match a literal '>'
""", "", line)
Regexes sind Spaß! Aber ich würde dringend empfehlen, ein oder zwei Stunden mit den Grundlagen zu verbringen. Für den Anfang müssen Sie lernen, welche Zeichen speziell sind: "Metazeichen", die maskiert werden müssen (dh mit einem umgekehrten Schrägstrich vor - und die Regeln unterscheiden sich innerhalb und außerhalb der Zeichenklassen ausgezeichnetes Online-Tutorial unter: www.regular-expressions.info . Die Zeit, die Sie dort verbringen, wird sich vielfach bezahlt machen. Viel Spaß beim Ausregen!
str.replace()
führt feste Ersetzungen durch. Verwenden Sie stattdessen re.sub()
.
Ich würde so gehen (Regex in Kommentaren erklärt):
import re
# If you need to use the regex more than once it is suggested to compile it.
pattern = re.compile(r"</{0,}\[\d+>")
# <\/{0,}\[\d+>
#
# Match the character “<” literally «<»
# Match the character “/” literally «\/{0,}»
# Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «{0,}»
# Match the character “[” literally «\[»
# Match a single digit 0..9 «\d+»
# Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
# Match the character “>” literally «>»
subject = """this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>.
and there are many other lines in the txt files
with<[3> such tags </[3>"""
result = pattern.sub("", subject)
print(result)
Wenn Sie mehr über Regex erfahren möchten, empfehle ich das Regular Expressions Cookbook von Jan Goyvaerts und Steven Levithan.
Der einfachste Weg
import re
txt='this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. and there are many other lines in the txt files with<[3> such tags </[3>'
out = re.sub("(<[^>]+>)", '', txt)
print out
die replace-Methode von String-Objekten akzeptiert keine regulären Ausdrücke, sondern nur feste Strings (siehe Dokumentation: http://docs.python.org/2/library/stdtypes.html#str.replace ).
Sie müssen das Modul re
verwenden:
import re
newline= re.sub("<\/?\[[0-9]+>", "", line)
muss keinen regulären Ausdruck verwenden (für Ihre Beispielzeichenfolge)
>>> s
'this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from 1-100</[99>. \nand there are many other lines in the txt files\nwith<[3> such tags </[3>\n'
>>> for w in s.split(">"):
... if "<" in w:
... print w.split("<")[0]
...
this is a paragraph with
in between
and then there are cases ... where the
number ranges from 1-100
.
and there are many other lines in the txt files
with
such tags
import os, sys, re, glob
pattern = re.compile(r"\<\[\d\>")
replacementStringMatchesPattern = "<[1>"
for infile in glob.glob(os.path.join(os.getcwd(), '*.txt')):
for line in reader:
retline = pattern.sub(replacementStringMatchesPattern, "", line)
sys.stdout.write(retline)
print (retline)