如何使用正则表达式

时间:2018-03-30 16:26:45

标签: python regex

我试图验证两个第一个&两个在带有正则表达式的文件中持续一行的字符。

我尝试过这个以及其他很多东西,但它没有用。我该怎么办?

regex = r"^[.B]?{2}"
regexEnd = r"[);]?{2}$"
regexC = re.compile(regex)
regexC1 = re.compile(regexEnd)

for filename in os.listdir(path1):
    f = gzip.open(path1 + filename, "rb")
    for line in f:
        if regexC.search(line) is not None & regexC1.search(line is 
        not None):
            file = open("db.txt", "w")
            file.write(line)

先谢谢你们:)

2 个答案:

答案 0 :(得分:0)

虽然我同意在这里使用索引可能比较简单 是一个正则表达式解决方案,匹配换行符之前的前两个和最后两个字符。注意:简单索引不会直接涵盖多行情况,其中字符串中间包含换行符,对于此特定问题似乎不是这种情况,但可能与将来参考相关。

from re import compile as re_compile, match, MULTILINE

text = "test\nwell"
regex = re_compile("^(?P<first>..).*(?P<last>..)$", MULTILINE)

print(match(regex, text))
print(match(regex, text).group("first"))
print(match(regex, text).group("last"))

答案 1 :(得分:0)

所以你在技术上可以用正则表达式做到这一点,但不建议你这样做,因为你只是检查两个字符是否等于某事。

如果你想使用正则表达式:

pattern = r"^\.B.*\);"
regex = re.compile(pattern)

for filename in os.listdir(path1):
    f = gzip.open(path1 + filename, "rb")
    for line in f:
        if regex.match(line):
            file = open("db.txt", "w")
            file.write(line)

您不需要实际拥有两个不同的正则表达式,您可以看看是否以.B开头,后跟任何内容,然后以);结尾。

要做的另一件事就是如果你对这些表达不满意并且做了类似的事情,那就一起避免使用正则表达式

for filename in os.listdir(path1):
    f = gzip.open(path1 + filename, "rb")
    for line in f:
        if line[:2] == ".B" and line[-2:] == ");"
            file = open("db.txt", "w")
            file.write(line)

这会创建一个要直接比较的字符串切片。它基本上表示line[:2]将所有字​​符排成一行,但不包括第二个索引,看看它是否等于“.B”。然后line[-2:]获取行的最后两个字符并比较它们以查看它们是否等于“);”