I've read through the following threads for help on using lookaheads and lookbehinds in regular expressions:
return string with first match Regex
Regex lookahead, lookbehind and atomic groups
Regular expression to match last number in a string
Consider the following string:
string = "About 17 or 18 minutes."
At the moment I know how to grab the last numeric (18) using "(\d+)(?!.*\d+)"
, but I'm having trouble grabbing the first numerical occurrence (17) using a single regular expression instead of re.findall()
or re.search()
methods.
Here are my attempts. I really hope I'm not too far off...:
1.(?<![0-9])\d+
2.(?<!.*\d+)(\d+)
(Invalid...)
I'm testing these on https://pythex.org/ FYI. Any help is very much appreciated, thank you!
EDIT: I've been informed in the comments section that (\d+) can be used and to just isolate the first match. Thank you for the help.