+ 2

Could u plz Explain this Code what happening here ?

Code 1: pat =re.compile(r'(\w+) (\w+)') pat.search("hello world") O/P:<_sre.SRE_Match object; span=(0, 11), match='hello world'> code 2: pat =re.compile(r'(\w) (\w)') pat.search("hello world") O/P:<_sre.SRE_Match object; span=(4, 7), match='o w'> In Above regular Expression codes , First Code pattern Alphanumeric with one or more character <white space> another one alpnum value it returns Hello World as Output But in Second Code i place Expression As (\w) it allows only one alphanum value so it gives output : o(form hello) w (from world) my question y last char return from hello and 1st char return from World

26th Jan 2017, 4:55 PM
Shiva M
Shiva M - avatar
1 ответ
+ 2
I think it is because.... if the pattern is '2 characters separated by a space', the only substring matching this in "hello world" is indeed "o w"
26th Jan 2017, 10:33 PM
ifl
ifl - avatar