Regex related to * and + in python -


i new python. didnt understand behaviour of these program in python.

import re sub="dear" pat="[aeiou]+" m=re.search(pat,sub) print(m.group()) 

this prints "ea"

import re sub="dear" pat="[aeiou]*" m=re.search(pat,sub) print(m.group()) 

this doesnt prints anything.

i know + matches 1 or more occurences , * matches 0 or more occurrences. expecting print "ea" in both program.but doesn't.

why happens?

this doesnt prints anything.

not exactly. prints empty string of course didn't notice, it's not visible. try using code instead:

l = re.findall(pat, sub) print l 

this print:

['', 'ea', '', ''] 

why behaviour?

this because when use * quantifier - [aeiou]*, regex pattern matches empty string before every non-matching string , empty string @ end. so, string dear, matches this:

*d*ea*r*  // * pattern matches. 

all *'s denote position of matches.

  • d doesn't match pattern. match empty string before it.
  • ea matches pattern. next match ea.
  • r doesn't match pattern. match empty string before r.
  • the last empty string empty string after r.

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -