Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression which matches a pattern, or is an empty string

I have the following Regular Expression which matches an email address format:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$ 

This is used for validation with a form using JavaScript. However, this is an optional field. Therefore how can I change this regex to match an email address format, or an empty string?

From my limited regex knowledge, I think \b matches an empty string, and | means "Or", so I tried to do the following, but it didn't work:

^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$|\b 
like image 596
Curtis Avatar asked Jul 26 '10 09:07

Curtis


People also ask

What is the regular expression for the empty string?

Thus the empty string "" always matches A* for any A . Here are some examples. These regular expressions are matched by the following strings: as_then_b matches any string of zero or more a 's, followed by any single character, followed by a b.

How check if a regular expression is empty?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.

What is regular expression in pattern matching?

A regular expression is a pattern of text that consists of ordinary characters, for example, letters a through z, and special characters. Matches pattern anywhere in the name. Marks the next character as either a special character, a literal, a back reference, or an octal escape.

Can an empty set be in a regular expression?

∅, the empty set, is a regular expression. ∅ represent the language with no elements {}.


1 Answers

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
  • Between \w and $
    • i.e. at the end of the string if it ends with \w

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?
like image 93
polygenelubricants Avatar answered Sep 29 '22 21:09

polygenelubricants