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|\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$ Works with the following URLs: // // // // // // // // // 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