Introduction to regular expression

1 min


Regular expressions are used to validate email addresses, username, phone numbers and other stuff. The syntax of regular expression is different in different languages. For example in perl it is differnt and in POSIX the regular expression syntax is different. The regular expression is also called regex.

Regular Expression
Regular Expression

Regular expressions are interpreted differently by different computer languages and operating systems. Let me discuss some parts of common regular expressions.

  • . or dot is used to match any single character. For example do.g will match doog or doag.
  • * is used to match zero or more of previous item. For example doo*g will match dog or dooog.
  • + is used to match at least one of previous item. For example doo+g will match doog and dog will not match.
  • ? is used to match zero or one of previous item. For example doo?g will match dog or doog.
  • | is used as an or operator. For example x|y matches x and also matches y.
  • ^ is used to identify beginning of your data. For example ^dog  matches dog but not mydog.
  • $ is used to identify end of your data. For example dog$ matches mydog but not matches dogmy.
  • () is used to create an item. For example b(a|o)ll will match ball or boll.
  • [] is used to create a list of items. For example [xyz] will create a list of x, y, z.
  • – is used to extend your list. For example [a-z] mean characters that match  a to z and [0-9] means digits from 0 to 9.
  • \ is used to escape some recognized characters. For example cat\? mean cat?.

You can use \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b to match email address in you text or page.

Like it? Share with your friends!

Junaid Rehman

I am a blogger and freelance web developer by profession. I love to blog and learn new things about programming and IT World.