0

I want to find just numbers in textfile so I made this code

r"[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?"

but I also get and numbers from string with characters (e.g. my txt file include string a278, and it also find number 278, so I want to not find that kind of numbers)

I want to find just "clear numbers", not a numbers from string which include char.

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • You can use whitespace boundaries on the left and right `(?<!\S)[+-]? *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?(?!\S)` https://regex101.com/r/kWQ9js/1 – The fourth bird Dec 30 '20 at 12:39

2 Answers2

0

You can consider look at wordboundaries. https://www.regular-expressions.info/wordboundaries.html

InspectorGadget
  • 879
  • 6
  • 20
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Sabito stands with Ukraine Dec 30 '20 at 12:06
0

You could solve such a problem with list comprehension, even without regex, as a simpler solution. Would have been beneficial if you'd gave us an idea of the type of data you're dealing with i/e of your input data.

Either way considering what you've stated, you want only numbers to be detected without string numbers.

case = "test123,#213 12" output = [int(i) for i in case .split() if i.isdigit()]

output Out[29]: [12]

Ecko
  • 115
  • 9