简体   繁体   English

python正则表达式匹配一个组或不匹配它

[英]python regex match a group or not match it

I want to match the string:我想匹配字符串:

from string as string从字符串作为字符串

It may or may not contain as .它可能包含也可能不包含as

The current code I have is我目前的代码是

r'(?ix) from [a-z0-9_]+ [as ]* [a-z0-9_]+'

But this code matches a single a or s .但是此代码匹配单个as So something like from string a little will also be in the result.所以类似from string a little也会出现在结果中。

I wonder what is the correct way of doing this.我想知道这样做的正确方法是什么。

You may use您可以使用

(?i)from\s+[a-z0-9_]+\s+(?:as\s+)?[a-z0-9_]+

See the regex demo查看正则表达式演示

Note that you use x "verbose" (free spacing) modifier, and all spaces in your pattern became formatting whitespaces that the re engine omits when parsing the pattern.请注意,您使用了x “verbose”(自由间距)修饰符,并且您的模式中的所有空格都变成了re引擎在解析模式时忽略的格式化空格。 Thus, I suggest using \\s+ to match 1 or more whitespaces.因此,我建议使用\\s+来匹配 1 个或多个空格。 If you really want to use single regular spaces, just omit the x modifier and use the regular space.如果您真的想使用单个常规空格,只需省略x修饰符并使用常规空格。 If you need the x modifier to insert comments, escape the regular spaces:如果您需要x修饰符来插入注释,请转义常规空格:

r'(?ix) from\ [a-z0-9_]+\ (?:as\ )?[a-z0-9_]+'

Also, to match a sequence of chars, you need to use a grouping construct rather than a character class.此外,要匹配字符序列,您需要使用分组构造而不是字符类。 Here, (?:as\\s+)?在这里, (?:as\\s+)? defines an optional non-capturing group that matches 1 or 0 occurrences of as + space substring.定义一个可选的非捕获组,匹配 1 或 0 次出现的as + 空格子字符串。

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

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