Title
Ruby String and Regular Expressions
Body

In Ruby use of Regular expression is simple and easy to use. They integrate with strings seamlessly... you can use regular expressions to search and find substrings within them. 2 methods of string which are very well used are match and scans

Match method

Match method is used to return the first occurance of the required expression within the string. The same can be used to search a string or a pattern of the string.

e.g.

"helloworldhello".match(/hello/) => <MatchData "hello">

"helloworldhello".match(/hello[a-zA-z]/) => <MatchData "hellow">

Scans method

Use the Match method to return the first possible match of the regular expression, but if you want mulitple matches, then its best to use the scans method. Scans method provides the multiple occurances of the matched expressions

e.g. 

"helloworldhello".scan(/hello/) => <MatchData "hello","hello">


use the scans and match methods when you are unsure of the string you want to search but you want to search based on a pattern.


RlingCMS uses scans method to scan all the patterns to identify the variables in the email templates and replace them with the dynamic code. All RLingCMS email templates will have code in the format of <RLING::USER::LOGIN> which has to be replaced with the actual user's login...

 

Tags
Associated Tags
ruby, string, rlingcms, scans, match
Created By
admin
Created At
2011-05-04 10:46:39 UTC
Last Updated At
2011-05-05 07:05:10 UTC
Add Comment