简体   繁体   中英

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 .

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 . So something like from string a little will also be in the result.

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. Thus, I suggest using \\s+ to match 1 or more whitespaces. If you really want to use single regular spaces, just omit the x modifier and use the regular space. If you need the x modifier to insert comments, escape the regular spaces:

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+)? defines an optional non-capturing group that matches 1 or 0 occurrences of as + space substring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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