jontyc
jontyc

Reputation: 3495

Regex - extract the numbers only - unless "..." is encountered

Consider a variable length string starting with a variable number of digits, then a non-digit, then anything.

Eg. 283432478($#*a433sd

(ignore SO's coloring)

Here I would like to extract the digit part 283432478. No big deal.

However sometimes this string is very large and a portion in the middle has been manually and haphazardly replaced by ...

Eg. 23445678404325jkla#$s23k$#$     =>     2344567840...3k$#$

By haphazardly I mean it could happen anywhere within the string, nor does it result in a fixed length string.

I would like the same regular expression to fail matching if it sees a ....

Any suggestions?

Upvotes: 1

Views: 414

Answers (1)

codaddict
codaddict

Reputation: 455152

You can try:

^([0-9]+)(?!.*\.\.\.)

See it

^            - Start anchor
([0-9]+)     - Capture one more digits
(?!.*\.\.\.) - Negative lookahead to ensure a ... is not present. Since . is a 
               regex meta-char, you need to escape it to mean a literal period.

Upvotes: 4

Related Questions