Standalone numbers Regex?

I currently use this regex:

(d+)

the problem that i can get 2 strings:

“2112343 and alot of 4.99”

OR

“4.99 and alot of 2112343 “

I get this from both:

[2112343, 4, 99]

I need to get only the 2112343
How can i achieve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using lookaround, you can restrict your capturing to only digits which are not surrounded by other digits or decimal points:

(?<![0-9.])(d+)(?![0-9.])

Alternatively, if you want to only match stand-alone numbers (e.g. if you don’t want to match the 123 in abc123def):

(?<!S)d+(?!S)

Method 2

try this

(?<!S)d+(?!S)

this will only match integers

Method 3

If I understand you right, you want to match those numbers with a point inside, too, but dont want to have these in the resulting collection.

I would approach this via 2 steps, first select all numbers, also those with a dot:

(d+(?:.d+)*)

then filter out everything that is not purely numbers, and use your first regex and apply it to each item of the resulting collection from the first step:

(d+)

Method 4

As I posted in my comment:

(?:^| )(d+)(?:$| )

It will match all “words” that are entirely composed of digits(a word being a string of non-space characters surrounded by space characters and or the beginning/end of the string.)

Method 5

Try this

(?<![0-9.])d+(?![0-9.])

It usees the pattern

(?<!prefix)position(?!suffix)

where (?<!prefix)position means: Match position not following prefix.

and position(?!suffix) means: Match position not preceeding suffix.

finally [0-9.] means: Any digit or the decimal point.

Method 6

>>>r = re.match("d+", "23423 in 3.4")
>>>r.group(0)
'23423'


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x