繁体   English   中英

正则表达式查找特定模式之间的所有字符串

[英]regex to find all the strings between certain patterns

我的输入字符串可以是以下几行之一:

Active App: Coffee (priority 34)

Active App: Hot Bread (priority 20)

Active App: Hot Baked Bread (priority 1)

etc...

在这种情况下,它可以是任何字符串 [a-zA-Z](一个或多个单词),而不是“ Coffee ”。

在“ (priority 34) ”中,只有整数会改变。

那么我如何从这条线上获得“ Coffee ”/“ Hot Bread ”/“ Hot Baked Bread ”?

我无法正确处理单词之间的空格。

这是一个简单的 python regex match()解决方案:

它会忽略您要捕获的应用程序名称之后的字符串部分。 但如果重要的话,可以添加。

它将捕获直到它看到( ,然后从字符串中删除尾随的空白字符。

import re;

myStr = "Active App: Hot Baked Bread (priority 34)";
appStr = re.match("Active App: ([^\(]*)", myStr);
print(appStr.group(1).rstrip());

这是一个仅捕获实际“活动应用程序”名称的版本,之后无需修剪字符串。 并且还会在打印之前检查是否找到匹配项:

import re;

myStr = "Active App: Coffee Some (priority 34)";
appStringMatch = re.match("Active App: (.*)\s\(", myStr);
if appStringMatch:
    print(appStringMatch.group(1));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM