Click here to Skip to main content
Click here to Skip to main content

Regular Expressions Meta Characters Usage with Examples

, 26 Apr 2006
Rate this:
Please Sign up or sign in to vote.
Regular Expressions Usage with Examples

Introduction

Regular Expressions are one of those that programmers frequently fumble with. Regular Expression patterns can be used either to manipulate the strings or for validations. So here are some of the expressions that would come in handy.

Regular Expressions are extensively used in UNIX shell programming using grep, sed compilers. A Regular Expression will have a set of literals and the following meta-characters.

Meta-Characters in Regular Expressions

. Matches any single character

[ ] Matches a single character contained in the brackets

Examples

  • [anil] matches character ‘a’ or ‘n’ or ‘i’ or ‘l’
  • [a-z] matches any lowercase alphabet between a to z
  • [Q-Z] matches any uppercase alphabet between Q to Z
  • [0-9] matches any digit between 0 to 9

To match the meta-characters [ or ] it needs to be inside the enclosing brackets.

[[]a-zA-Z], matches any upper or lower case alphabet along with square brackets.

Hyphen ‘-’ can be matched if it is at the end or beginning of the square brackets.

  • [-a-z] matches any alphabet between a to z or ‘-‘
  • [acf-i-] matches characters ‘a’ or ‘c’ or ‘f’ or ‘g’ or ‘h’ or ‘i' or ‘-‘

[^] Matches a single character that is not in the brackets.

Example

  • [^anil] matches any character other than ‘a’ or ‘n’ or ‘i’ or ‘l’.

^    Matches the beginning of the test string.  

$    Matches the end of the string.

*    Matches 0 or more copies of the predecessor, which could be a character or a sub expression like [a-p].

Examples

  • [a-p]* matches “” or “a” or “abc” or “p” etc.
  • A* matches “” or “A” or “AA” or “AAA” etc.

+    Matches 1 or more copies of the predecessor, which could be a character or a sub expression like [a-p].

Examples

  • [a-p]+ matches  “a” or “abc” or “p” etc.
  • A+ matches “A” or “AA” or “AAA” etc.

Expressions to Manipulate/Process Strings

Regex.Replace(“HowYouDoing”, "[A-Z]+", " $&"); 

Inserts space in front of each upper case letter in the string.

Result would be “How You Doing”.

Expressions to Validate Strings

Regex regex = new Regex(“[^0-9]”);   
ReturnValue = Regex.IsMatch(<TestString>);  

ReturnValue will have true if the <TestString> is not a natural number, otherwise false. Helps if you want to identify whether the <TestString> is a decimal or a character string.

Regex regex = new Regex(“^[0-9]*$”); 
ReturnValue = Regex.IsMatch(<TestString>);  

ReturnValue will have true if the <TestString> is a positive natural number, otherwise false.

Regex regex = new Regex(“^-[0-9]+$|^\+?[0-9]+$”); 
ReturnValue = Regex.IsMatch(<TestString>);  

ReturnValue will have true if the <TestString> is a positive natural number, otherwise false.

History

  • 26th April, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Anil Choudary

United States United States
No Biography provided

Comments and Discussions

 
NewsThere's a new version of the RegEx Tester Tool ! PinmemberBucanerO_Slacker1-Mar-08 23:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 26 Apr 2006
Article Copyright 2006 by Anil Choudary
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid