To match pattern
or an empty string, use
^$|pattern
Explanation
^
and$
are the beginning and end of the string anchors respectively.|
is used to denote alternates, e.g.this|that
.
References
- regular-expressions.info/Anchors and Alternation
On \b
\b
in most flavor is a “word boundary” anchor. It is a zero-width match, i.e. an empty string, but it only matches those strings at very specific places, namely at the boundaries of a word.
That is, \b
is located:
- Between consecutive
\w
and\W
(either order):- i.e. between a word character and a non-word character
- Between
^
and\w
- i.e. at the beginning of the string if it starts with
\w
- i.e. at the beginning of the string if it starts with
- Between
\w
and$
- i.e. at the end of the string if it ends with
\w
- i.e. at the end of the string if it ends with
References
- regular-expressions.info/Word Boundaries
On using regex to match e-mail addresses
This is not trivial depending on specification.
Related questions
- What is the best regular expression for validating email addresses?
- Regexp recognition of email address hard?
- How far should one take e-mail address validation?