regex - python group(0) meaning -


what exact definition of group(0) in re.search?

sometimes search can complex , know supposed group(0) value definition?

just give example of confusion comes, consider matching. printed result def. in case group(0) didn't return entire match.

 m = re.search('(?<=abc)def', 'abcdef') >>> m.group(0) def 

match_object.group(0) says whole part of match_object chosen.

in addition group(0) can be explained comparing group(1), group(2), group(3), ..., group(n). group(0) locates whole match expression. determine more matching locations paranthesis used: group(1) means first paranthesis pair locates matching expression 1, group(2) says second next paranthesis pair locates match expression 2, , on. in each case opening bracket determines next paranthesis pair using furthest closing bracket form paranthesis pair. sounds confusing, that's why there example below.

but need differentiate between syntax of paranthesis of '(?<=abc)'. these paranthesis have different syntactical meaning, locate bound '?<='. main problem don't know '?<=' does. called look-behind means matches part behind expression bounds.

in following example 'abc' bound look-behind.

no paranthesis needed form match group 0 since locates whole match object anyway.

the opening bracket in front of letter 'd' takes last closing bracket in front of letter 'f' form matching group 1.

the brackets around letter 'e' define matching group 2.

import re  m = re.search('(?<=abc)(d(e))f', 'abcdef')  print(m.group(0)) print(m.group(1)) print(m.group(2)) 

this prints:

def de e


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -