Escaping Parentheses in Regex

If you want to match one of a set of character, it’s best to use a character class. And within such a class, most escaping rules don’t apply. So to match a dot, comma, slash or closing parenthesis, you can use

Regular expression that matches valid IPv6 addresses

I’m having trouble writing a regular expression that matches valid IPv6 addresses, including those in their compressed form (with :: or leading zeros omitted from each byte pair). Can someone suggest a regular expression that would fulfill the requirement? I’m considering expanding each byte pair and matching the result with a simpler regex.

Regex for “AND NOT” operation

I’m looking for a general regex construct to match everything in pattern x EXCEPT matches to pattern y. This is hard to explain both completely and concisely…see Material Nonimplication for a formal definition. For example, match any word character (\w) EXCEPT ‘p’. Note I’m subtracting a small set (the letter ‘p’) from a larger set … Read more

What is the difference between square brackets and parentheses in a regex?

These regexes are equivalent (for matching purposes): /^(7|8|9)\d{9}$/ /^[789]\d{9}$/ /^[7-9]\d{9}$/ The explanation: (a|b|c) is a regex “OR” and means “a or b or c”, although the presence of brackets, necessary for the OR, also captures the digit. To be strictly equivalent, you would code (?:7|8|9) to make it a non capturing group. [abc] is a … Read more

Regular Expressions- Match Anything

How do I make an expression to match absolutely anything (including whitespaces)?Example: Regex: I bought _____ sheep. Matches: I bought sheep. I bought a sheep. I bought five sheep. I tried using (.*), but that doesn’t seem to be working.