IT Community - Software Programming, Web Development and Technical Support

Ruby Regular Expression

This is a discussion on Ruby Regular Expression within the Ruby forums, part of the Web Development category; Hi, Could any one explain about the ruby regular expression...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-23-2008, 08:19 PM
nnraja nnraja is offline
D-Web Programmer
 
Join Date: May 2007
Posts: 94
nnraja is on a distinguished road
Default Ruby Regular Expression

Hi,
Could any one explain about the ruby regular expression
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 08:21 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

Hi raja,
these are the regular expression
] range specificication (e.g., [a-z] means a letter in the range a to z)
\w letter or digit; same as [0-9A-Za-z]
\W neither letter or digit
\s space character; same as [ \t\n\r\f]
\S non-space character
\d digit character; same as [0-9]
\D non-digit character
\b backspace (0x08) (only if in a range specification)
\b word boundary (if not in a range specification)
\B non-word boundary
* zero or more repetitions of the preceding
+ one or more repetitions of the preceding
__________________
Shaalini.S
Be the Best of Whatever you are...

Last edited by shaalini : 04-23-2008 at 08:36 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 08:22 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

{m,n} at least m and at most n repetitions of the preceding
? at most one repetition of the preceding; same as {0,1}
| either preceding or next expression may match
() grouping
The main reason regular expressions exist is this: if you want your program to check if "something" looks like something else, in terms of similar characters or spacing or lenght or many many other things you might think of, you use a regular expression. The table above will give you a concise view of all Ruby's regular expression elements.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-23-2008, 08:23 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

Regular expressions are used for matching certain patterns, so for example, if you use \d then this will try to match a digit, if you use \s, this will match a space character.

Let me now give you an example on how you use regular expressions:

johnsays = “It's 9:18PM here now.I cannot wait to go out tonight.”
time = /\d\d:\d\d/
if johnsays=~time
puts “John told you what time it is.He is eager to go out.”
else
puts “John does not care what time it is.He is too tired to go out.”
end
__________________
Shaalini.S
Be the Best of Whatever you are...

Last edited by shaalini : 04-24-2008 at 01:49 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-23-2008, 08:24 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

The =~ is the match operator.

Basically, what this code does is this:

In the variable johnsays you store a string, which in this case contains a string of chars indicating an hour in standard format also.The variable time stores the pattern to search for: in this case, 2 digits, followed by a semicolon, followed by another 2 digits.Notice you you need to put a forward slash / before the first \d and how you also need to a add a backwards slash after the last \d You need to do this each time you want to place the patterns to search for inside a variable.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-23-2008, 08:24 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

There is another way of using regular expressions.Since Ruby is an object-oriented programming language, the "johnsays" expression becomes a member of the Regexp class, which is the regular expressions class in Ruby.

Therefore, you could also write the expression as

johnsays = Regexp.new(‘\d\d:\d\d’)
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-23-2008, 08:27 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby Regular Expression

using the .new method for the Regexp class.

Whenever a match is found, a number of variables are created by Ruby.

Here are some examples with added details:

puts “Characters before the match : #{$`}”

puts “Matched characters : #{$&}”

or

puts “Matched characters : #{$~}”

puts “Characters after the match : #{$’}”

puts “Tird part of the match : #{$3}”
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 07-13-2008, 01:41 AM
kamal12 kamal12 is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 3
kamal12 is on a distinguished road
Default Re: Ruby Regular Expression

let's not forget that this was the case with virtually any new programming language and framework - until someone believed in it and realized it's true potential and decided to participate in the community and contribute with code or valuable advice.
.Actually, there probably isn't one "best than all" programming language or framework,
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is regular expression? saravanan PHP Programming 8 03-28-2008 06:12 AM
How to use Regular expression in Java? S.Vinothkumar Java Programming 3 11-28-2007 08:45 PM
Regular expression Sathish Kumar C# Programming 23 09-10-2007 06:00 AM
Can any one give regular expression for this string? raj PHP Programming 1 07-24-2007 11:42 PM
Regular expression vadivelanvaidyanathan Perl 0 07-15-2007 06:21 PM


All times are GMT -7. The time now is 09:57 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0