0

I have a pretty basic regex for a soundcloud link as follows:

/^(https?:\/\/)?((www)?\.)?soundcloud\.com\/[a-zA-Z0-9]*/

It doesn't do much and I would like to block that kind of more complicated links that contain something else (?fbclid=) than only valid soundcloud information:

Invalid:

https://soundcloud.com/beat-one/done-lam/w-avlwV?fbclid=IwAR8TyDI-1ezXHrP1BfaBG1rLP8gTlEQeGJzKO0DGZl7h1i63VWqenV_yDI8

Valid:

https://soundcloud.com/beat-one/done-lam/w-avlwV

How can I do it?

Thanks!

2
  • Do you mean like this? ^(https?:\/\/)?((www)?\.)?soundcloud\.com\/(?!.*\?fbclid=).*$ regex101.com/r/NRxwHz/1 Note that as www is optional is could also match .soundcloud Commented Dec 13, 2019 at 12:47
  • Thank you very much for your answer, it seems a little complicated to understant as i am quite a regex noob. Could you just specify that the link should not contain any ?character? Commented Dec 13, 2019 at 12:51

1 Answer 1

2

Using a pattern like ((www)?\.)? matches optional www but could also match an optional .

I think you meant to use optional www. as a whole.

For the question mark part, you could match the beginning of the use and then use a negative lookahead (?!.*\?fbclid=) to assert what is on the right does not match ?fbclid=

If that is the case, you could match for example 0+ times a non whitespace char \S*

If you are not using the capturing groups for further processing, you could also make them non capturing (?:

^(https?:\/\/)?(www\.)?soundcloud\.com\/(?!.*\?fbclid=)\S*$

Regex demo

If the link should not contain a question mark, you could simplify the lookahead to (?!.*\?)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, but https://soundcloud.com/beat-one/done-lam/w-avlwV?fbclid=IwAR8TyDI-1ezXHrP1BfaBG1rLP8gTlEQeGJzKO0DGZl7h1i63VWqenV_yDI8 still matches your regex unfortunately
@Wizzardzz Did you check the regex demo? regex101.com/r/EHIfa5/1 How are you using the pattern? What is the tool or language?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.