Martyn Ball
Martyn Ball

Reputation: 4885

Regex, match anything unless just numbers

I have got the following regex expression so far:

used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*)

This is sort of working, I need it to match basically ANYTHING apart from JUST numbers after used-cars/

Match:

used-cars/page-1
used-cars/1eeee
used-cars/page-1?*&_-

Not Match:

used-cars/2
used-cars/400

Can someone give me a hand? Been trying get this working for a while now!

Upvotes: 1

Views: 44

Answers (1)

Rahul
Rahul

Reputation: 2738

There are few shortcomings of your regex used-cars\/((?:\d+[a-z]|[a-z]+\d)[a-z\d]*).

It's checking for used-cars/ followed by multiple digits then one character within a-z OR multiple characters within a-z then one digit.

[a-z\d]* is searching for either characters or digits which is also optional.

It's inaccurate for your pattern.

Try with following regex.

Regex: ^used-cars\/(?!\d+$)\S*$

Explanation:

  • used-cars\/ searches for literal used-cars/
  • (?!\d+$) is negative lookahead for only digits till end. If only digits are present then it won't be a match.
  • \S* matches zero or more characters other than whitespace.

Regex101 Demo

Upvotes: 3

Related Questions