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,044
  • 5
  • 25
  • 50

2 Answers2

0

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

dolmushcu
  • 844
  • 4
  • 16
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
  • 105
  • 7