Regular Expressions: An Introduction
by Amit Arora
Published: January 12, 2004
User Rating: 8.9 (274 votes)

Page Page » 1 2 3 4 5 6 7 8 9

Character Class

Character class is a set of characters which are grouped together and used for matching A CHARACTER. In simple terms it would mean "match a character against any one of these (set of) characters". Its that simple. But how we use these, unlocks there power.

Character class or character set is defined by putting the characters in the the square brackets. []

Also if the characters you want to use in the sequence, then you don't even have to type them all, just type the first character, followed by dash (-), followed by the last character. This called defining a character range.

Regex /[aeiou]*/ Explaintaion Match "a" or "e" or "i" or "o" or "u", any number of times or match any vowel Regex /[a-z]*/ Explaintaion Match "a" till "z". Match only alphabets Example what True Are False you True doing? False ALLCAPS False 1948 A.D. False 300 B.C. False Mach3 False Regex /[a-zA-Z]*/ Explaintaion Same as above but matches both lower case and upper case Example what True are True you True doing? False ALLCAPS True 1948 A.D. False 300 B.C. False Mach3 False Regex /[0-9]*/ Explaintaion Match only decimal numbers Example 1948 True 400 BC False 2365994 True 313-256-5864 False +1-208-456-7890 False Regex /[+\-0-9]*/ Explaintaion Match decimal numbers or "+" or "-" Example 1948 True 400 BC False 2365994 True 313-256-5864 True +1-208-456-7890 True

Negitive character class is like a character class but the characters are EXCLUDED for matching. In simple terms it would mean "match a character which is NOT FROM these (set of) characters".

Character class or character set is defined by putting the characters in the the square brackets and starting with a caret. [^]

Regex /[^0-9]*/ Explaintaion Match anything except decimal numbers Example 1948 False 400 BC True 2365994 False 313-256-5864 True +1-208-456-7890 True
Page Page » 1 2 3 4 5 6 7 8 9

About

Amit Arora is web developer with expertise in developing eCommerce enabled websites for the businesses.

Contact | Resume

Subscribe to newsletter




Get Firefox

Monitored by Site24x7
Uptime