ए भाई Think ज़रा हटके
Note: amtyThumb must be installed for new version of amty thumb post/recent

Regular Expressions: Common elements part 2 (Range Search)


Regular Expression

I had covered following components in last chapter of common elements

  1. ^(Start),
  2. $(End),
  3. .(Any char),
  4. *( zero or more occurrences),
  5. +( one or more occurrences) and
  6. ?( zero or one occurrences)
What if you need to limit your search up to some characters only? For example, you have to search for mobile number.

Range Search

Range search let you search for specified characters or their range. You need to enclose all characters with in square brackets as follows

 	[abcd987ABCD] 

Example

        [0123456789]+

You can use above RE for mobile number searching. However, there are two things wrong with this RE.

1) If characters increased then length of RE is increased
2) Mobile numbers are generally 10 digits. While this RE will search all numeric words contain at least 1 digit.

Lets resolve these issues,

1
Define range to decrease RE length. You can define numeric and character range as follow

[0-9] It means any character from 0 to 9.
[a-z] any character from a to z.
[A-Z] any character from A to Z.

You can limit the range like ,[01] or [A-J].
Or you can combine them as [a-z0-9A-U].
You can also use some special characters, back slashes characters and spaces like [a-z@\. \t]

Examples
1. Search for all CAPITAL Words.

     [A-Z]+ 

2. Search for Moblie numbers

 	[0-9]+ 

3. Search for email ids (An email id can contain “.”,”_”. And website name can contain “-“ )

 	[a-z0-9\_\.]+@[a-z0-9\-]+\.[a-z]+ 

If you add “^” just after “[“ then it search for exclusion of characters. For example;

 [^0-9]* 

Above RE will search for every character excluding numeric chars.

2
Fix number of occurrences

*, + are used to set minimum occurrence. We can define maximum or minimum limit of occurrences of RE.

r{m} exact m occurrences of r
r{m,} at least m occurrences of r
r{n,m} n to m occurrences of r

Examples

Mobile number has 10 fixed digits.

	[0-9]{10}

Domain name should be less than 5 characters.

 	[a-z0-9\_\.]+@[a-z0-9\-]+\.[a-z]{2,5}
Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

92
views


To book below area mail me




captcha

You can follow any responses to this entry through the RSS 2.0 feed.