在给定模式之后匹配符号之间的模式

时间:2022-12-05 21:25:42

I would like to find the first occurrence of a string, after a certain other string, and between a certain pattern. The documents that I am parsing are not xml, but have similar rules of start/end. and example of what I would be looking at:

我想找到第一次出现的字符串,在某个其他字符串之后,以及某个模式之间。我正在解析的文档不是xml,但具有类似的开始/结束规则。以及我将要看的内容的例子:

...b.filext", "{xxxxx-xxx-xxx-xxx-xxxxxxxxxxx}"

and I am trying to get the string between { and }, right after an occurrence of a vcxproj, every time it happens in the document. I tried the following, but I get a list of None:

并且我试图在vcxproj发生之后立即在{和}之间获取字符串,每次它都发生在文档中。我尝试了以下内容,但是我得到了一个无列表:

my_list=[]
for line in text.split('.vcxproj'):
    if '{' in line: 
        my_list.append(re.match( r"(?<=\{)(.*?)(?=\})", line))

I have tried to alter my expression but no success. Help ? Thank you.

我试图改变我的表达但没有成功。帮帮我 ?谢谢。

2 个解决方案

#1


1  

How about:

怎么样:

    re.findall(r"vcxproj.*?\{(.*?)\}", text)

#2


2  

re.match matches only at the beginning of the string. Try re.search. Alternatively, replace the loop with re.findall.

re.match仅匹配字符串的开头。尝试re.search。或者,用re.findall替换循环。

#1


1  

How about:

怎么样:

    re.findall(r"vcxproj.*?\{(.*?)\}", text)

#2


2  

re.match matches only at the beginning of the string. Try re.search. Alternatively, replace the loop with re.findall.

re.match仅匹配字符串的开头。尝试re.search。或者,用re.findall替换循环。