Sunday, May 24, 2009

Regular expression Ruby

Hi Guys,

This post is about my stint on regular expressions using Ruby, programming language. Ruby is one of the powerful languages that provide excellent support to Regular expressions.

There are several method available for String class for checking the patterns. I am gonna write about few of the methods available and also that I know.

Now let’s talk about how can ensure that email is in right format. so let’s take an email id barrack.obama@gmail.com.
Now our aim is to find the word before and after the symbol @. Ruby provides a method for scanning through string using regular expression called scan.


s = ‘barrack.obama@gmail.com’
s.scan /[\w+\.]{2,20}/


The above expression will result in an array as following,
[‘barrack.obama’, ‘gmail.com’]

See how easy it is. This expressions are not limited to ruby language, this is the same for all languages. This expression will work in most cases.

Let me explain the expression I have written above. [\w+\.]. what this says is that it will try to match to match words that includes underscore and dot. {2,20} means that the word that gets matched should have the length of 2 to 20.

This expression will help you to get the domain name of the user this email address belong to.

The scan method will always result in array, which might not be what we want all the time. There can be situations where we want to check if the string matches the certain pattern like if or while or case loops

For those cases, ruby provides a way to do that using a new operator =~.

Let’s assume that we want to check if the file has an extension .jpg.
image = ‘bacd.jpg’

you can always do like


image =~ /\w+\.(JPG)$/i


The expression mentioned above matches any string that starts with and ends with .jpg irrespective of the case.

There is one powerful ruby method for searching and replacing the set of words called ‘gsub’. Let’s assume we have a word ‘hello123’ and we want to replace 123 with ABC. This can be accomplished easily with gsub. The following syntax


litter = ‘hello123’
litter.gsub(/\d+/, ‘ABC’)


The above statements will result in ‘helloABC’. There are lots of things in ruby which I haven’t explored yet. Those may follow next in the post.

That’s all for this post I have...

5 comments:

khelll said...

It's good also to cover new things in ruby 1.9

dan said...

Hi Antony

Nice post, i'm a big fan of the flexibility of regular expressions and ruby has nice integration.

I'd probably solve your examples a little bit differently.

1) s.scan /[^@]+/
#we want to match atleast one but as many characters that aren't '@' as possible

2) /^.+\.JPG$/i
#we want .jpg to be at the end of the string, your example would match strings like "bacd.jpg.bak" but not "bacd .jpg" (valid on windows)

Antony said...

Cool thoughts dude.

Anonymous said...

謝謝您囉~~看您的分享文章是個很好的經驗~~.................................................................

Curt Lennix said...



GET THE BEST HACKING SERVICE‼️
PYTHONAX HACKS
Motto-: Hacking Just Got Easier

Get to hire a Hacker from one of the best Hacking groups there is. PYTHONAX are a group of talented hackers who have been Hacking in secret for almost a decade now. When we take up a Hacking Job, you have the assurance that it’s will be done. We stayed in hidden but the numbers of False Hackers has increased over the years and this has caused us to come out in the open.

Here Are List Of Hacking Services We Offer-:

▪️Phone Hacking & Cloning

▪️Email Hacking

▪️Social Media Hacking(Facebook, Instagram e.t.c)

▪️Computer Hacking

▪️Deleted Files & Documents Recovery

▪️Breach Detection

▪️Website Hacking

▪️Tracking using GPS and Spyware

▪️Deleted Mails and Text messages Recovery

OTHER SPECIAL HACKING SERVICES

▪️Binary Option Recovery

▪️Scam Money Recovery

▪️Bitcoin Multiplication

▪️Change Of Grades In Universities/Colleges

▪️Phone Calls Monitoring

▪️And lots more...........

For more enquire, contact us via the emails below
Pythonaxservices@gmail.com
Pythonaxhacks@gmail.com

Pythonax
2020 © All Right Reserved.