7 Cases quick Walkthrough on Regular Expressions

Regular expressions might be the common term among us in programming and we more or less felt the need to do tasks sometimes with it and yes, it’s a bit confusing as well to undestand. But once anyone can grab the rule , surely will found the sweetness in it. Here in this article i have tried to mention some points/case that we may need very often. I took help of the site Regexr for demonstration, really a nice place to practice regular expression.

Case 1 : Selecting All

Let’s say ,  you want to select only ‘Welcome’ word from the string below. Do this, ‘/.+/g’ , and you are done.

1

Here, the period ( ‘.’ ) means any one character and ‘+’ after that means more than one. So, in combined, ‘.+’ means one or more character to select. Note that, a white space is also a character.

Case 2 : Select All Uppercase Characters

Suppose, you need to the uppercase characters only, try ‘/[A-Z]+/g’, result will be like this,

2

Here, [A-Z] means , any uppercase character that is From A to Z is to be selected. And ‘+’ means match till the uppercase character is found. If you omit ‘+’, each uppercase letter will be considered individual match for this expression. Same way, ‘a-z’ for lowercase match , ‘0-9’ to match the number

Case 3 : Not Selecting one or more specific character

To prevent any character from selecting/matching the pattern, simply add ‘^’ before that character. In this example below i wanted to not select the white space. So i did it ,

3

It means “Regular expression ! Select all the characters except the ones i mentioned in [] right after the ‘^’ symbol”, pretty cool, right ?

Case 4 : Select any character(s) that matches a given character set

Let’s think, you want to select only the individual charactes that are present in the given character set. Write it this way,

4

Here ‘[ELLHj]’ is the set of characters that you provided and the matches are highligted here. If you gave + after that , same match result would return but not as individual characters

 

Case 5 : Matching More Than One Group

You can perform more than one matching operation in more than one group. Following examples check two matches. One is ‘HELLO’ and another one is ‘LL’

6

Case 6 : Look Ahead And Look Behind

Consider, you want to select a string or character that is just before a specific charater. For example, i want to select the string that is just before ‘!’ , i did it so,

7

.+(?=!) means select any one or more character that is just before ‘!’ (? = !) If i wanted the string that is after the ‘!’, i would write /(?<=!).+/g

 

Case 7 : Selecting characters with optional one

Suppose, you want to select a character set but one of them is optional to be there. In the example bellow i wanted to select the the word ‘https’, but even if there was the word ‘http’ instead of ‘https’, that would be selected cause, the character ‘s’ has been declared as optional by the symbol ‘?’ after it.

8

That’s all for now. Hope you would get a clear understanding on the cases discussed here, but these are not all, just a quicksatart though. You can learn and practice through many tutorials on the internet. The links given below might be helpful for you to know more.

http://www.regular-expressions.info/tutorial.html

http://regexone.com

JS regular expressions : http://www.w3schools.com/js/js_regexp.asp

 

Leave a Reply

Your email address will not be published. Required fields are marked *

thirteen − six =