Dec-16-2020, 07:16 PM
(This post was last modified: Dec-16-2020, 07:18 PM by matt_the_hall.)
Hello,
I'm trying to get a better understanding of regex capturing groups, because my python script is not executing as expected, based on what I understand of how regex works. I am using re.compile to set up the pattern, and then pattern.find
I am trying to capture the following phrases from user input to then send to At to set an At job:
1 minute
2 hours
4 days
etc.
for
I'm trying to get a better understanding of regex capturing groups, because my python script is not executing as expected, based on what I understand of how regex works. I am using re.compile to set up the pattern, and then pattern.find
I am trying to capture the following phrases from user input to then send to At to set an At job:
1 minute
2 hours
4 days
etc.
for
r'(\b\d+\sminute|hour|day|week|month|year)'1 minute works, but 1 hour does not. The capture with 1 hour is 'hour', omitting the preceding 2. But, if I change the capture group to something like
r'(\b\d+\s(minute|hour|day|week|month|year))', I end up with a tuple, which causes other problems. My understanding is that if you have a group within a group, then regex matching should only match the whole group, but after going around and around in circles with different little changes to the regex syntax, I am stumped. Am I stuck with a tuple that I then have to pull apart and deal with, or do I have to set up the capture so it makes a list of individual characthers, like [1, ' ', m, i, n, u, t, e] and then flatten the list? I am thinking there must be a more elegant way of capturing something so simple as 1 minute vs 2 hours without a whole bunch of post-processing workaround garbage. I should note, too, that I had an optional (s?) as a part of the syntax originally, but I've backburnered that smaller problem until I can get a better grip on regex capturing groups. Perhaps my issue is not in the pattern set up but in the .findall method???