简体   繁体   中英

How to search part of pattern in regex python

I can match pattern as it is. But can I search only part of the pattern? or I have to send it separately again. eg pattern = '/(\\w+)/(.+?)' I can search this pattern using re.search and then use group to get individual groups. But can I search only for say (\\w+) ? eg

pattern = '/(\w+)/(.+?)'
pattern_match = re.search(pattern, string)
print pattern_match.group(1)

Can I just search for part of pattern. eg pattern.group(1) or something

You can make any part of a regular expression optional by wrapping it in a non-matching group followed by a ? , ie (?: ... )? .

pattern = '/(\w+)(?:/(.+))?'

This will match /abc/def as well as /abc .

In both examples pattern_match.group(1) will be abc , but pattern_match.group(2) will be def in the first one and an empty string in the second one.

For further reference, have a look at (?:x) in the special characters table at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

EDIT

Changed the second group to (.+) , since I assume you want to match more than one character. .+ is called a "greedy" match, which will try to match as much as possible. .+? on the other hand is a "lazy" match that will only match the minimum number of characters necessary. In case of /abc/def , this will only match the d from def .

That pattern is merely a character string; send the needed slice however you want. For instance:

re.search(pattern[:6], string)

uses only the first 6 characters of your pattern. If you need to detect the end of the first pattern -- and you have no intervening right-parens -- you can use

rparen_pos = pattern.index(')')
re.search(pattern[:rparen_pos+1], string)

Another possibility is

pat1 = '/(\w+)'
pat2 = '/(.+?)'
big_match = re.search(pat1+pat2, string)
small_match = re.search(pat1, string)

You can get more innovative with expression variables ($1, $2, etc.); see the links below for more help. http://flockhart.virtualave.net/RBIF0100/regexp.html https://docs.python.org/2/howto/regex.html

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