Hans Daigle
Hans Daigle

Reputation: 364

Regex to finding any kind of uuid or random generated text

I want to find any kind of uuid or random generated text in a url path and replace it with <random>. Examples :

  1. /test/ajs1d5haFkajs1dhasdd2as345sdAS3+Ddas9 = /test/<random>
  2. /test/akKd9Ja3/ajs1d5haFkajs1ddasd623ha5sdAS3Ddas9=/30 = /test/<random>/<random>/30
  3. /test/akKd9Ja3/Example-ASDAdddasd-108174.js = /test/<random>/Example-108174.js.
  4. /test/akKd9Ja3-ASj83asj-dask92qwe_ke = /test/<random>

I'm looking for a solution that will match on a string:

This is what I used so far : /[a-zA-Z0-9-=_+\s]{30,}

This works for most cases since uuids are often longer than 30 char. But I don't catch the small ones i.e /5c88148/ or /6qdkKdk5/. I also match on things like Example-ASDAddasd-108174.js.

Upvotes: 0

Views: 660

Answers (1)

Code Maniac
Code Maniac

Reputation: 37755

Update - In case you want match must contain at least one digit.You can use this.

(?<=\/)(?=[\w-+=\s]+[0-9])[\w-+=\s]{7,}(?![.])(?!\.)(?=\/|\n)

Demo for update

You can try this.

(?<=\/)[\w-+=\s]{7,}(?!\.)(?=\/|\n)

Explanation

  • (?<=\/) - Positive look behind. Matches '/'.
  • [\w-+=\s]{7,} - Matches any word character, -,+,=, and space 7 or more time.
  • (?!\.) - Negative look ahead. Do not match ..
  • (?=\/|\n) - Positive look ahead. Matches '/' or '\n'(New line).

Demo

Upvotes: 1

Related Questions