如何用regex提取一行包含一列的版本号

2024-05-17 03:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个下面的文件,如何提取行是有5.6版本号。我需要找到正则表达式的模式

A   KB           1024               MYsql root
B   GB            1                 ELK-6
C   KB           1024               Mysql5.6
E   KB           1024               ELK
F   GB           1                  Mysql5.6

预计出局

C   KB           1024               Mysql5.6
F   GB           1                  Mysql5.6

我的熊猫代码

import re
infile = r'C:\user\Desktop\m.txt'
df_list = []
with open(infile) as f:
    for line in f:
        # remove whitespace at the start and the newline at the end
        line = line.strip()
        # split each column on whitespace
        columns = re.split('\s+', line, maxsplit=4)
        df_list.append(columns)
df = pd.DataFrame(df_list)
df[df[3].str.contains("5.6")]

我需要用Python处理regex,而不是用dataframe。我没有给熊猫贴标签


Tags: columns文件thereelkdfkbline
2条回答

regex将匹配5.6版本的所有字符串。你知道吗

^.*5\.6$

不幸的是,我不喜欢熊猫,但是:

re.search(r'5\.6', s)

如果字符串“5.6”存在于字符串sNone中的任何位置,则返回一个match object,其计算结果为True;如果不存在,则返回None,其计算结果为False

相关问题 更多 >