如何通过Python在属性文件中使用带有正则表达式字符串的正则表达式模式进行搜索

问题描述

我确实有一个带有Key = Value Pair的属性文件

属性文件名称=“ johhny_johhny_yes_papa.properties”

prop.org.size = 5
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org

我确实有一个像下面这样的字符串列表-

List_papa = ['org','johnny','jimmy','yes','papa']

我的目标是搜索==等号后属性文件中上面列表中存在的字符串,并打印该行。

我的代码-

ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','papa']
with open(ENV_PROP) as f:
    file_content = f.read()
    contents = file_content.split('\n')
for line in contents:
    for user in List_papa:
        if (re.findall("\\b"+user+"\\b",line)):
            print(line)

我的输出-

prop.org.size = 5
user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org

实际上正在打印所有内容,因为我的if条件在所有行中都是True。

我的预期输出是-

user.id.name = johnny
name.account.id = jimmy
cassandra.user.name = yes
kakfa.user.name = org

我想在属性文件的=(等于)符号后搜索列表元素。

我在正则表达式下得到了这个

(?<==).+$

上面提到的Regex会让我在“ =”(equalto)符号后的字符串。

所以我尝试使用上面的代码应用此正则表达式-

更新的代码-

ENV_PROP = "johhny_johhny_yes_papa.properties"
List_papa = ['org','papa']
with open(ENV_PROP) as f:
    file_content = f.read()
    contents = file_content.split('\n')
for line in contents:
    regex_line = re.findall(r"(?<==).+$",line)
    for user in List_papa:
        if (re.findall("\\b"+user+"\\b",regex_line)):
            print(line)

我收到以下错误消息。

TypeError: expected string or bytes-like object

我上面的代码有什么问题,请为我提供正确的代码

解决方法

使用const data = { "status": [ ["aa","bb","cc","dd"],["ee","ff","gg","hh"],["ii","jj","kk","ll"] ] } const array2 = { "name": "status","data": ["cc","gg"] } console.log('Before') console.log(data.status) data.status = data.status.filter(row => row.some(elem => array2.data.includes(elem))) console.log('After') console.log(data.status)可以将多个参数作为元组传递给str.endswith

例如:

str.endswith

输出:

List_papa = ('org','johnny','jimmy','yes','papa')   #Note! List_papa is a tuple
with open(filename) as infile:
    for line in infile:
        line = line.strip()
        if line.endswith(List_papa):
            print(line)