0
Python - Small question
Hello, I was learning about regular expressions - lesson 83.1 - Python core And the in the lesson there was a question: ....................................................................... Which of these patterns would not re.match the string "spamspamspam"? 1- spamspam 2- sp 3- pamspam The correct answer is pamspam ........................................................................ I understand that Python matches the patters with the first letter ok but how is (1- sp) write?? I tried it at a terminal and it didn't match, can anyone explain to me how did the question consider it matching and the terminal didn't Thank you for helping me Have a great day!!
6 ответов
+ 8
Karim Maasarani
using 'sp' as a pattern will match:
import re
pattern = r"sp"
if re.match(pattern, "spamspamspam"):
print("Match")
else:
print("No match")
[EDITED]:
the matching result will be:
> <re.Match object; span=(0, 2), match='sp'>
> to inspect how regex is working, the site: *regex101.com* is recommended.
+ 2
@Sandeep the 1- is the first answer number and 2- is the second answer number
+ 2
Karim Maasarani okay :)
did you resolve your doubt?
+ 1
I don't know a lesson
-lesson 83.1-
Really with - at the beginning?
+ 1
what is (1-sp)?
I think there is some confusion regarding the question.
String is present in the question and patterns are given in the options. Question asks which of the pattern will not match the string when using re.match().
re.match() works if the match is found at the beginning of the string.
In the cases above,
1) spamspam is present in the beginning of the string '**spamspam**spam'
2) same as above 'sp' is also present in the beginning of the string '**sp**amspamspam'
3) pamspam is present in the string but the match does not start from the beginning, match starts from second alphabet.
's**pamspam**spam'
and hence re.match() will not match the string.
this link might be helpful:
https://www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-re-search-vs-re-match/
+ 1
@Lothar thanks i'll visit the site