Regex problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Regex problem (/thread-35708.html) |
Regex problem - bontwoody - Dec-04-2021 Im having some difficulty understanding regex patterns. After reading around I constructed the following pattern. ['^.?RE..D$'] My understanding of this was: ^ indicated the start of the word .? allowed zero or one character RE were just that, the letter R and E .. means exactly two characters D as per RE $ indicated the end of the word. As per my understanding I expected words such as ORE, FRETED and REDED to pass but not words such as RENDED, DOPED or REPRO. Ive obviously got this totally wrong, can anyone explain how and suggest the way in which I can achieve this sort of pattern? Thanks in advance RE: Regex problem - Gribouillis - Dec-04-2021 ^ and $ are the beginning and the end of the string, not the word. See Python's regular expression syntax RE: Regex problem - bontwoody - Dec-04-2021 (Dec-04-2021, 05:59 PM)Gribouillis Wrote: ^ and $ are the beginning and the end of the string, not the word. See Python's regular expression syntax Thanks, but in my scenario there is only one word in the field so I dont think that matters. Something screwy is going on in my code as I just isolated the search and it works as expected so i dont think my understanding if off at all, so thats a relief! Ill dig deeper and see if I can find my error. Thanks again RE: Regex problem - nilamo - Dec-04-2021 Quote:As per my understanding I expected words such as ORE, FRETED and REDED to pass but not words such as RENDED, DOPED or REPRO. Why would ORE pass? There's no D in it. Aside from that one, all your other examples pass/fail as you described. If you're not seeing that, then there's an issue with the code, not the regex. When it comes to testing regex, I like using https://www.regexpal.com/ If you turn on the multiline flag, you can add all your examples on their own line, and FRETED and REDED light up, since they match. It helps to quickly see if a regex is doing what you think it is. ...but your regex is doing what you described it as doing. This one is pretty simple, it's when you start using things like negative lookahead groups where they can get very confusing. |