Monday, May 29, 2017

Regular Expressions

[abc] - Either 'a' or 'b' or'c'
[^abc] - Except 'a' or 'b' or'c'
[a-z] - Any lower case alphabets
[A-Z] - Any upper case alphabet symbol
[a-zA-Z] - Any alphabet symbol
[0-9] Any digit from 0 to 9.
[a-zA-Z0-9] - Any alphanumeric character
[^a-zA-Z0-9] Except Any alphanumeric character.(Allows only special characters)

\s - Space Character
\S - Any character Except Space
\d - Any digit from [0-9]
\D - Any character Except digit
\w - Any word character(Any alphanumeric)[a-zA-Z0-9]
\W - Except Word character(only special characters)
. - Any symbol including special characters.

a - Exactly one a
a+ - Atlest one a
a* - Any number of a's including Zero Number also
a? - Atmost one a

Eg: if the string is "abaabaaab", then

a - Exactly one a, gives
0-a,1-a,3-a,5-a,6-a,7-a

a+ - Atlest one a, gives
a,aa,aaa

a* - Any number of a's including Zero Number also
a,aa,aaa

a? - Atmost one a. Means either 0 time or 1 time
a, a,a,a,a,a

\s - divides "Gorrepati Leela Prasad" as 'Gorrepati' 'Leela' 'Prasad'
\. - divides "www.google.com" as 'www' 'google' 'com'
[.] - divides "www.google.com" as 'www' 'google' 'com'

Questions:
1. Write Regular Expression to represent all 10 digit Mobile Numbers
[7-9][0-9]{9}

2. 10 digit OR the number contains 11 digits then the first digit should be 0
0?[7-9][0-9]{9}

0? - represents atmost 1 occurance of 0, means 0 is optional in the begining, if any number exists then it must be 0.

3. 10 digit OR 11 digit OR 12 digit
(0|91)?[7-9][0-9]{9}

4. Regex for e-mail id

email is durga_java.76@tv9.co.in

Rules:
1. emailid should start with alphanumeric character.
2. rest of the id can be alphanumeric  and . or _ are the only 2 allowed special characters.
3. @ followed by alphanumeric that represents 'tv9' and should occur once and .co.in where .and alphabatic characters and reoccur multiple times.

so.

[a-zA-Z0-9][a-zA-Z0-9._]*@[a-zA-Z0-9]+([.][a-zA-Z0-9])*

[a-zA-Z0-9]+ Represents atleast 1 occurance of alphanumeric 'tv9'
([.][a-zA-Z0-9])* Represents re-occurance of alphabatic characters '.co.in'

only for gmail id's then,

[a-zA-Z0-9][a-zA-Z0-9._]*@gmail[.]com


5. Few scenarios,

Conditions,
a. The allowed characters are a-z,A-Z,0-9,# and $.
b. atleast 2 characters in length.
c. 1st character should be Lower case alphabetical symbol from a to k.
d. 2nd character should be digit either (0,3,6,9)

[a-k][0369][a-zA-Z0-9#$]*

all names with starts with a|A

[aA][a-zA-Z]*

all names those ends with l or L.

[a-zA-Z]*[lL]

all names those starts with a|A and ends with l or L.

[aA][a-zA-Z]*[lL]

No comments:

Post a Comment