Regular Expression (Regex)

Regex is powerful when you want to select a dynamic text with certain patterns and go on from there. Regex looks complicated to me at first but when you went through some of it long enough, eventually you will get a grasp of what’s happening. (Applied to me on learning any new things. You just need to stare at it LONG enough)

Syntax that useful to be remembered:

Basic Regular Expressions(Found this cool spreadsheet from here):

Regex Cheat Sheet


Ruby Basic Syntax:

sampletext = “This is a sample text”

Select text: MATCH

sampletext.match(\[^a]\)  =>  This is a sample text (all except a)

Select and replace: GSUB

sampletext.gsub!(/[aeiou]/, ‘*’)   => Th*s *s * s*mpl* t*xt

sampletext.gsub(/[ep]/, ‘e’ => ‘E’, ‘p’ => 9)  => This is a sam9lE tExt

** The difference of gsub and gsub! is result will affect the original variable **

Useful tools/sites for regular expressions:

1. Ruby XP

I feel uncertain whether the regex that I just type out successfully selected what I want to be selected or not so this is a life saver to me. The user interface does not look attractive and outdated but I use this a lot rather than Rubular because besides strings comparison, it also has GSUB option (which replace function)


  • Your regular expression (required): Your regex
  • String comparison (required): Sample paragraph/ sentences for testing
  • Replacement for sub/gsub (optional): Characters that you want to replace

2. Rubular

Similar with rubyxp with better UI and provided quick guidances.


3. Tutorials from nettuts (Ruby)

Useful for people who is prone to videos tutorials(me) rather than text tutorials. It gives you a head start concept of how things works. My mind understands the concept (sort of) but my fingers froze and unsure where to start.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s