Python Forum
trying to use/understand the "re"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to use/understand the "re"
#4
(Mar-18-2019, 03:18 PM)gentoobob Wrote: I want to only search for numbers only, not numbers with text. So "12" by itself, not "test12". Just the standalone numbers. Any help is greatly appreciated. Thank you!
The need to Lookahead and Lookbehind as it called in regex,to make sure that numbers are alone and not in string.
>>> import re
>>> 
>>> output = "12 test12 13 test13 14 test14 101 test101 102 test102"
>>> r = re.findall(r"(?<!\S)\d+(?!\S)", output)
>>> r
['12', '13', '14', '101', '102']
Reply


Messages In This Thread
trying to use/understand the "re" - by gentoobob - Mar-18-2019, 03:18 PM
RE: trying to use/understand the "re" - by snippsat - Mar-18-2019, 05:03 PM

Forum Jump:

User Panel Messages

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