Edgar
Edgar

Reputation: 1120

Regex selecting separate numbers

How it is possible to select numbers which are seperater from characters for example in such cases:

1221 ; =123 ; >156 ; != 56

and ignore in shuch ases:

asd446 ; das64adsa ; 5465sdad ; aasd59.status

Upvotes: 1

Views: 42

Answers (2)

Saleem
Saleem

Reputation: 8978

Try this regex:

((?<=;)[+\-=><! ]+\d+|^\d+)\s*(?=(?:;|$))

Sample input text:

1221 ; =123 ; >156 ; != 56
asd446 ; das64adsa ; 5465sdad ; aasd59.status
sdkd 55 2

After running on sample, it matches

1221
=123
>156
!= 56

Upvotes: 0

Jan
Jan

Reputation: 43169

As already said in the comments, you could either use word boundaries like so:

\b\d+\b

Or, in general lookarounds (negative/positive lookahead/behind).
See a demo on regex101.com.

Just for training purpose, you could as well use the already mentionned lookarounds:

(?<=^|[=>\s])\d+(?=$|[\s])

This says: look for the start or =, > or a whitespace behind and make also sure that what immediately follows, is either the end of the string ($) or a whitespace.

Upvotes: 1

Related Questions