Regex for youtube URL

Here’s a regex I use to match and capture the important bits of YouTube URLs with video codes: ^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$ Works with the following URLs: https://www.youtube.com/watch?v=DFYRQ_zQ-gk&feature=featured //www.youtube.com/watch?v=DFYRQ_zQ-gk www.youtube.com/watch?v=DFYRQ_zQ-gk //youtube.com/watch?v=DFYRQ_zQ-gk youtube.com/watch?v=DFYRQ_zQ-gk //m.youtube.com/watch?v=DFYRQ_zQ-gk m.youtube.com/watch?v=DFYRQ_zQ-gk https://youtube.com/watch?v=DFYRQ_zQ-gk%3Ffs%3D1%26hl%3Den_US https://youtube.com/watch?v=DFYRQ_zQ-gk%3Ffs%3D1%26hl%3Den_US //www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US www.youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US youtube.com/v/DFYRQ_zQ-gk?fs=1&hl=en_US https://youtube.com/watch?v=DFYRQ_zQ-gk%3Fautoplay%3D1 //www.youtube.com/embed/DFYRQ_zQ-gk www.youtube.com/embed/DFYRQ_zQ-gk //youtube.com/embed/DFYRQ_zQ-gk youtube.com/embed/DFYRQ_zQ-gk https://www.youtube-nocookie.com/embed/DFYRQ_zQ-gk?autoplay=1 https://www.youtube-nocookie.com/embed/DFYRQ_zQ-gk http://www.youtube-nocookie.com/embed/DFYRQ_zQ-gk //www.youtube-nocookie.com/embed/DFYRQ_zQ-gk www.youtube-nocookie.com/embed/DFYRQ_zQ-gk https://youtube-nocookie.com/embed/DFYRQ_zQ-gk http://youtube-nocookie.com/embed/DFYRQ_zQ-gk //youtube-nocookie.com/embed/DFYRQ_zQ-gk youtube-nocookie.com/embed/DFYRQ_zQ-gk //youtu.be/DFYRQ_zQ-gk youtu.be/DFYRQ_zQ-gk https://www.youtube.com/HamdiKickProduction?v=DFYRQ_zQ-gk The captured … Read more

“Unknown escape sequence” error in Go

The \ backslash isn’t being interpreted by the regex parser, it’s being interpreted in the string literal. You should escape the backslash again: regexp.Compile(“[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+”) A string quoted with ” double-quote characters is known as an “interpreted string literal” in Go. Interpreted string literals are like string literals in most languages: \ backslash characters aren’t included … Read more

Sed to extract text between two strings

sed -n ‘/^START=A$/,/^END$/p’ data The -n option means don’t print by default; then the script says ‘do print between the line containing START=A and the next END. You can also do it with awk: A pattern may consist of two patterns separated by a comma; in this case, the action is performed for all lines … Read more

How to use RegEx in Dart?

This is a more general answer for future viewers. Regex in Dart works much like other languages. You use the RegExp class to define a matching pattern. Then use hasMatch() to test the pattern on a string. Examples Alphanumeric final alphanumeric = RegExp(r’^[a-zA-Z0-9]+$’); alphanumeric.hasMatch(‘abc123’); // true alphanumeric.hasMatch(‘abc123%’); // false Hex colors RegExp hexColor = RegExp(r’^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$’); … Read more