0

What's wrong with my code? Lifetime's supply of pizzas on me if you help me out with this one (emoji-flavored, though 😀😀😀)

So I am trying to understand this group stuff about regular expressions, and decided to try writing out the code as rudimentarily as I could by substituting all the metacharacters (e.g. *,+,?) in terms of curly braces (because they seem to be a very general form of all these metacharacters). And it does work - it spits out the result just fine - it's just that it seems to work with everything. Which is good, because I got part of what I wanted, but bad because I don't seem to understand why. I wanted it to work just like the original code, but now it prints out everything. Why doesn't it work like the original code? The original code looks like this: import re pattern = r"egg(spam)*" if re.match(pattern, "egg"): print("Match 1") if re.match(pattern, "eggspamspamspamegg"): print("Match 2") if re.match(pattern, "spam"): print("Match 3") And the results are: >>>>>>>>>>> Match 1 Match 2 >>>>>>>>>>> I've written it down my modified code here so you can see: import re pattern = r"(((e){0,}(g){0,}(g){0,})){0,}((s){0,}(p){0,}(a){0,}(m){0,}){0,}" if re.match(pattern, "egg"): print("Match 1") if re.match(pattern, "eggspamspamspamegg"): print("Match 2") if re.match(pattern, "spam"): print("Match 3") if re.match(pattern, "((s){0,}(p){0,}(a){0,}(m){0,}){0,}"): print("Match 4") if re.match(pattern, "45"): print("Match 5") And the results are: >>>>>>>>>> Match 1 Match 2 Match 3 Match 4 Match 5 >>>>>>>>>> I had expected that it wouldn't print Match 3, but it did. I also added in some others - one without any letter used in "eggspam" and another with numbers in string format - but it still printed them out even if had I written out my raw string normally, it doesn't print them out. If all the parts are exactly the same in my weird raw string as those in the normal string, why doesn't my code not print out Match 3 - 5? Moreover, why does the code work in the first place? I am assuming that it doesn't work exactly the same as t

9th Jul 2018, 2:59 PM
Patrick
Patrick - avatar
2 odpowiedzi
+ 2
Those {0,} mean "any occurence count" same like '*' special character and this make all unuseful (you place they everywhere). Can you tell in simple words, what do you want obtain???
9th Jul 2018, 3:11 PM
KrOW
KrOW - avatar
+ 1
"{0,}" will return a match even if the string does not contain the character, that's why you're getting the output you are https://code.sololearn.com/cSLCBVMRlsxq/?ref=app
9th Jul 2018, 7:55 PM
hinanawi
hinanawi - avatar