Priyank Patel
Priyank Patel

Reputation: 6996

Explanation about a particular regular expression pattern

I am using Asp.Net/C# in my project.In one of my forms I am using Regular Expression Validator to validate an email address.I searched for some examples for validating email.I found this \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* Can anybody explain me this pattern , it would be really helpful. Any explanation is much appreciated.

Thanks.

Upvotes: 0

Views: 582

Answers (1)

Sascha
Sascha

Reputation: 10347

Some short hints:

  • \w stands for word character

  • [-+.'] for one of the characters in the braces

  • plus and * are quantifiers

  • ( ) surround a group which can create a backreference (something you could refer to or read programmatically)

A longer (automated) explanation:

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

Match a single character that is a “word character” (letters, digits, and underscores)     «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 1 «([-+.']\w+)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the capturing group itself.  The group will capture only the last  iteration.  Put a capturing group around the repeated group to capture all iterations. «*»
   Match a single character present in the list “-+.'” «[-+.']»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “@” literally «@»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 2 «([-.]\w+)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «*»
   Match a single character present in the list “-.” «[-.]»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “.” literally «\.»
Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the regular expression below and capture its match into backreference number 3 «([-.]\w+)*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «*»
   Match a single character present in the list “-.” «[-.]»
   Match a single character that is a “word character” (letters, digits, and underscores) «\w+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

I used RegexBuddy to created the longer, automated explanation

Edit:

To have some starter info on regular expressions you can take a look at http://www.regular-expressions.info/

Upvotes: 3

Related Questions