Skip to main content

Start using regex


The below definitions have been pulled from Regex Basics just to have a basic understanding before we start practising. I have just displayed some basic things used and would keep updating the post as the time permits.

Literal     A literal is any character we use in a search or matching expression, for example, to find ind in windows the ind is a literal string - each character plays a part in the search, it is literally the string we want to find.

Metacharacter     A metacharacter is one or more special characters that have a unique meaning and are NOT used as literals in the search expression, for example, the character ^ (circumflex or caret) is a metacharacter.

Target string     This term describes the string that we will be searching, that is, the string in which we want to find our match or search pattern.

Search expression     Most commonly called the regular expression. This term describes the search expression that we will be using to search our target string, that is, the pattern we use to find what we want.

Escape sequence     An escape sequence is a way of indicating that we want to use one of our metacharacters as a literal. In a regular expression an escape sequence involves placing the metacharacter \ (backslash) in front of the metacharacter that we want to use as a literal, for example, if we want to find (s) in the target string window(s) then we use the search expression \(s\) and if we want to find \\file in the target string c:\\file then we would need to use the search expression \\\\file (each \ we want to search for as a literal (there are 2) is preceded by an escape sequence \).

Elements in array are matched only once, only the first match is taken care of.
 => nil

 => #<MatchData "1">

 => "1"

 => #<MatchData "1">

Search for \\file9
 => #<MatchData "\\file9">

 => #<MatchData "9">

 => #<MatchData "9">

 => #<MatchData "9231">

 => #<MatchData "923-C">

 => nil

 => nil

The ^ (circumflex or caret) inside square brackets negates the expression
 => #<MatchData "923v">

 => #<MatchData "923f">

 => nil

The ^ (dollar) means look only at the start of the target string
"Mozilla fox".match('fox$')
 => #<MatchData "fox">

The $ (dollar) means look only at the end of the target string.
"fox Mozilla fox".match('^fox')
 => #<MatchData "fox">

The . (period) means any character(s) in this position, for example, ton. will find tons, tone and tonneau but not wanton because it has no following character.

 => nil

"wanton ".match('ton.')
 => #<MatchData "ton ">

?     The ? (question mark) matches the preceding character 0 or 1 times only, for example, colou?r will find both color (0 times) and colour (1 time).

 => #<MatchData "color">

 => #<MatchData "colour">


The * (asterisk or star) matches the preceding character 0 or more times, for example, tre* will find tree (2 times) and tread (1 time) and trough (0 times).

 => #<MatchData "treee">

 => #<MatchData "tree">

 => #<MatchData "tre">

Diff between * and +. * checks for the previous character 0 or more times whereas + checks for 1 or more time.

 => #<MatchData "tr">

 => nil

Post comments if you feel that some more regex basics should be added. I would love to update the post.


Popular posts from this blog

upload images to AWS::S3 in ruby using aws sdk gem

Using gem aws-sdk for a ROR application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application. The s3 keeps a limitation on the number of buckets creation whereas there is no limitation for content inside a bucket. This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public, so that the images uploaded are directly accessible. The input is takes is the image complete path where it is present, folder in which it should be uploaded and user_id for whom it should be uploaded. def save_screenshot_to_s3(image_location, folder_name,user_id) service = => ACCESS_KEY_ID, :secret_access_key => SECRET_ACCESS_KEY) bucket_name = "app-images" if(service.buckets.include?(bucket_name)) bucket = service.buckets[bucket_name] else bucket = service.buckets.create(bucket_name) …

Fresh server start after instance degraded

So the issue was, amazon issued an warning regarding the server to be terminated, before i could think of making an backup the instance stopped.

The troubleshoot operations like reboot, stop and start didn't worked.
Simple Steps to start a fresh instance with the previous content on server.