Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regex problem
#1
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
Reply
#2
^ and $ are the beginning and the end of the string, not the word. See Python's regular expression syntax
Reply
#3
(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
Reply
#4
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.
ndc85430 likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020