Asif Iqbal
Asif Iqbal

Reputation: 543

Validate url with extensios at the end using regular expression

How to validate the below url's which is ending with extensions like .mp4 or .wmv

And also starting with http or https or www

Example:

http://media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4
https://media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4
www.media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4

Upvotes: 2

Views: 1562

Answers (2)

netadictos
netadictos

Reputation: 7722

More readable:

^(http:\/\/|https:\/\/|www\.).*(\.mp4|\.mkv)$

More concise:

^(http(s)?:\/\/|www\.).*(\.mp4|\.mkv)$

Demo:

https://regex101.com/r/wK2rV0/1

Explanation:

  • First group verifies that the line starts "^" matching "http://"(with optional: https) or www.
  • in the middle, ".*", any given character different to newline
  • the last group, it must match or mp4 or mkv at the end of the line.

Upvotes: 3

Vladu Ionut
Vladu Ionut

Reputation: 8193

/^(http[s]?:\/\/)?([^:\/\s]+)(:([^\/]*))?(\/\w+\.)*([^#?\s]+)(\?([^#]*))?(\.mp4|\.mkv)$/gm

see demo here https://regex101.com/r/vL1gZ5/2

Upvotes: 2

Related Questions