Tuesday, September 23, 2008

Get started with 'Regular Expressions'

Ruby supports majority of standard regular expression syntax.
Regular expression is pattern that either does or does not match any given string.
A regular expression in ruby is placed between a pair of forward slashes: (/^./)

Basic Special characters and Symbols with in regular expressions:

'^' --> Anchor for the beginning of a line
'$' --> Anchor for the end of a line
'\A'-> Anchor for the start of a string
'\Z'-> Anchor for the end of a string
'.' -->Any character
'..'-> Any two characters
'\w'->Any letter, digit or underscore
'\W'->Any thing that '\w' does not match
'\d' ->Any digit
'\D' ->Any thing that '\d' does not match
'\s' -->White space
'\S'-->Non white space

Some examples:

To substitute first two characters of string :
"Hi Harini".sub(/^../,"Hello")
output : Hello Harini

Here '^' represents the beginning of a line, and '..' represents two characters. That means two characters starting from the line, that would be 'Hi' in our example. Method 'sub' will substitute the first argument with second.
Hi is substituted with Hello.

To substitite last six characters of a string :
"Hi Harini".sub(/......$/,"vamshi")
output : Hi vamshi

Here '$' represents the end of a line, and '......' represents six characters. That means six characters at the end of the line, that would be 'Harini' in our example. Method 'sub' will substitute the first argument with second.
Harini is substituted with vamshi.

Iterations :

To print single character at a time of a string:
"abcde".scan(/./){|x| puts x}
output :
a
b
c
d
e

Scan method looks through the string for anything that matches the regular expression passed to it.
'.' represents a single character, In our example regular expression looks for a single character at a time and prints them separately.

To print two characters at a time:
"Hi Harini, how are you".scan(/../){|x| puts x}
output :
Hi
H
ar
in
i,
h
ow
a
re
y
ou

Scan method looks through the string for anything that matches the regular expression passed to it.
'..' represents a two characters, In our example regular expression looks for a two characters at a time and prints them separately.
Note : '.' means any character that includes white spaces also.

To print two characters at a time with out including white spaces:
"This is a test".scan(/\w\w/) { |x| puts x }
output:
Th
is
is
te
st

Special characters denoted with back slashes have special meaning. '\w' means "any alphanumeric character or an underscore".
'\w' will consider the grouped characters.

To print digits in a given string:
"I have 20$".scan(/\d+/){|x| puts x}
output : 20

Regular expression that uses '\d' is to matchany digit, and the + that follows \d makes \d match as many digits in a row as possible.

with these examples try more with those listed in the table..

No comments:

About Me

Friendly and humanitarian Honest and loyal Original and inventive Independent and intellectual