Skip to content

Instantly share code, notes, and snippets.

@BrianHicks
Created May 3, 2013 16:58
Show Gist options
  • Save BrianHicks/5511242 to your computer and use it in GitHub Desktop.
Save BrianHicks/5511242 to your computer and use it in GitHub Desktop.
crazy regex syntax

Basics:

"three numbers" -> "\d{3}"
"an optional dot" -> "\.?"
"a letter and two numbers, three times" -> "(\w\d\d){3}"

So that can come together to form this:

"one to three numbers and an optional dot, four times" -> "(\d{1,3}\.?){4}"

It would be even cooler if you could define patterns and then reuse them.

"an IPV4 address" -> "(\d{1,3}\.?){4}"

Obviously this has to be a little different to actually produce a valid IPV4, but you get the idea.

(can't resist, it'd be "one to three numbers and a dot, three times, then one to three numbers")

@BrianHicks
Copy link
Author

oh, and [\d\w] would be like "a letter or a number"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment