piA2
piA2

Reputation: 43

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.

Upvotes: 2

Views: 78

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

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.

Upvotes: 1

Related Questions