简体   繁体   English

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

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

My input string can be one of the following lines:我的输入字符串可以是以下几行之一:

Active App: Coffee (priority 34)

Active App: Hot Bread (priority 20)

Active App: Hot Baked Bread (priority 1)

etc...

In this case, instead of " Coffee ", it could be any string [a-zA-Z] (one or more words).在这种情况下,它可以是任何字符串 [a-zA-Z](一个或多个单词),而不是“ Coffee ”。

In " (priority 34) ", only the integer would change.在“ (priority 34) ”中,只有整数会改变。

So how do I get the " Coffee "/" Hot Bread "/" Hot Baked Bread " from this line?那么我如何从这条线上获得“ Coffee ”/“ Hot Bread ”/“ Hot Baked Bread ”?

I am unable to properly handle the space between the words.我无法正确处理单词之间的空格。

Here's a simple solution with python regex match() for you:这是一个简单的 python regex match()解决方案:

It disregards the part of the string after the application name that you want to capture.它会忽略您要捕获的应用程序名称之后的字符串部分。 But that could be added, if important.但如果重要的话,可以添加。

It will capture untill it sees a ( , and then later strip the trailing whitespace character from the string.它将捕获直到它看到( ,然后从字符串中删除尾随的空白字符。

import re;

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

Here's a version that only captures the actual 'Active App' name, without the need to trim the string afterwards.这是一个仅捕获实际“活动应用程序”名称的版本,之后无需修剪字符串。 And also checks to see that a match was found before printing it:并且还会在打印之前检查是否找到匹配项:

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