Python 如何使用正则表达式提取包含版本号的列的行
我有一个下面的文件,如何提取具有5.6版本号的行。我需要找到正则表达式的模式Python 如何使用正则表达式提取包含版本号的列的行,python,regex,Python,Regex,我有一个下面的文件,如何提取具有5.6版本号的行。我需要找到正则表达式的模式 A KB 1024 MYsql root B GB 1 ELK-6 C KB 1024 Mysql5.6 E KB 1024 ELK F GB 1 M
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处理正则表达式,而不是用dataframe。我没有标记熊猫这将匹配5.6版本的所有字符串
^.*5\.6$
不幸的是,我不喜欢熊猫,但是: 关于搜索器'5\.6',s
将返回一个匹配对象,如果字符串“5.6”存在于字符串s中的任何位置,则该对象的计算结果为True;如果不存在,则返回一个匹配对象的计算结果为False。可能重复not with pandas、regex、with pandas我编写了代码您的输入是一个长字符串吗?否,它不长的stringpandas.Series.str.contains已经假定其pat为regex,这意味着pandas中的代码已经在使用regex。尝试添加Mysql root==Mysql5A6的行,str.contains5.6仍将捕获它。自动理解为regex。