Regular Expressions (Regex) Tutorial

 



Regular expressions can be utilized using the command line, and we have to use these by themselves so you can apply these for certain areas. Regular expressions allow us to search for specific patterns of text. There is so much you can do with Regular Expressions, and can match with any text you can think of. 


Make sure you have the .* tool because you have the search tool to use regular expressions. 

First of all we can search for characters. If I were to search for abc, it highlights the match in the lowercase alphabet, and it is case sensitive. 

There are characters that need to be escaped.

Typing period matches everything because . is a special character in regular expressions. To search for a period, you have to escape it and do a backslash as follows:

\.

To get a url you can do like chrischucs\.blogspot\.com to search this website.


. Any character except new line
\d Digit
\D not a digit
\w Word Character
\W not a word character
\s Whitespace
\S Not a whitespace
\b Word boundary
\B not a word boundary
^ Beginning of a String
$ End of a string
[] Matches Characters in Brackets
[^ ] Matches Characters NOT in bracket
| Either Or
( ) Group

To get a phone number, we match 3 digits in a row \d\d\d and either mash a dash or a dot then we search to add in more digits so we get \d\d\d.\d\d\d.\d\d\d\d. This is more useful for a literal search, because we are searching for a specific pattern.

What if we want to match anything with a dash or a dot? In a character set, we want to get the characters we want to match, a dash or a dot.

\d\d\d[-.]\d\d\d[-.]\d\d\d\d

This is how you do it.

Let's say we only want to match 800 and 900 numbers? How do we do this?

[89]00[-.]\d\d\d[-.]\d\d\d\d


If we want to specify a range between 1 and 7 we can simply do [1-7] or [a-z] if we want to match lowercase letters a through z.

We can go through ranges too.  For example, to go through the alphabet we do [a-zA-Z].

The caret shows that negates the set and matches everything that is not in the set.

[^a-z] is the way to do it. To show another example, if we have cat, mat, pat, and bat, if we want every word that ends in at except bat, we do this:

[^b]at

If we match any character \d\d\d.\d\d\d.\d\d\d\d we see the quantifiers that we have available.  

{3} exact number

{3,4} range of numbers (Minimum, Maximum)

.d{3}.\d{3}.\d{4}

Let's start by matchine the names that start with Mr.

Mr\.

We can use question mark quantifiers if we want to match a certain index of a specific characters like this:

Mr\.?\s[A-Z]\w

This matches space and uppercase letter or any other character as indicating from the questions. \w is matching any word character after the upper space. Using the + sign looks for one or more character after the uppercase character. Use the asterisk * if  we want to match something to match 0 or more characters. 


We can also match a character set to match r or s if we want to match Mr. or Mrs. but the best way I think we can do this is use a group.
 
Create a group like this, to match r or rs.

M(r|s|rs)\.?\s[A-Z]\w*

We are looking for an M followed by an r,s, and rs, followed by an optional period (\.?) and then match a space then a letter A to z, and then an optional word after this.


* -    0 or more
+ -   1 or more
? -   0 or One
{3} - exact number
{3,4} - range of numbers

Sample Regex

[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-].[a-zA-Z0-9-]+\,[a-zA-Z0-9-.]+

How do I get a regular expression that matches all email? 

[a-zA-Z]+@[a-zA-Z]+\.com

This is how you properly match addresses up.

Let's give 4 links:

https://www.google.com
http://coreyms.com
https://youtube.com
https://www.nasa.gov


And this is how we capture important information:

https?://(www\.)?\w+\.\w+

We want to group is the domain name, or the string of one more characters, so we want to get the domain and top level domain.

We can do something called a back reference to reference our group, and we can do something called replace the matches like the following: 

Group 1: $1

And this is how you replace the characters.
 
Group 1 is www. and the second group is the domain name, so we want to get group2 and group3 for the corresponding links. Knowing how to match groups with regular expressions can save a ton of time for doing things like this.

Comments

Popular Posts