Beginner’s Guide to Regular Expression (Regex)

Beginner’s Guide to Regular Expression (Regex)


A regular expression is a set of characters forming a pattern that can be searched for in a string. Regex can be used for validation like to validate credit card numbers, to search i.e. via complex text matches, and for replacement of the corresponding text with another channel. It also supports multiple languages ​​- learn it once and you can use it in many programming languages.

I have seen few people take the first look at the regular expression and ignore it altogether. I don’t blame them; The syntax of regex is complex and will make a lot of cringe just like these command line languages, only worse. But then, anything new is scary and seems impossible to learn at first. So, borrowing the words of Horatius, I will say this; Start, be bold and dare to be wise.

30 useful tools and resources for regular expressions

30 useful tools and resources for regular expressions

The regular expression, or often known as a regex, is a pattern made up of rules used to match a … Read more

Regex – An Introduction

Regex has its roots in neuroscience and mathematics and was only implemented in programming in 1968 by Ken thompson in the QED text editor for text search. It is now part of many programming languages ​​such as Perl, Java, Python, Ruby, and JavaScript.

Let’s look at some examples of how regexes work.

I’ll be using JavaScript in my examples. Now to pass the beginner level you have to learn all the characters, classes, quantifiers, modifiers and methods used in regex. Here is a link to Mozilla Developer Network Regular Expression Page where you can display a table containing all of these. You can also refer to the cheat sheet at the end of this article with the most used characters.

Let’s see a simple example with an explanation. This is a regex.

regex

This is what the regex above will look for in a line, a ‘B’ character followed by at least one of any character between (and including) ‘a’ through ‘z’, ‘A’ through ‘ Z ‘and the numbers 0 to 9.

Here’s an example of matches in a highlighted row:


Basket
, bulb,
B12
vitamin,
BaSO4
, NOT
Before Christ
business

The regular expression above will stop the search at
Basket
and return a positive response. It’s because the global modifier ‘g‘must be specified if you want the regular expression to examine all possible matches.

Now let’s see how to use this expression in JavaScript. the test method goes: if found a return of match true, other false.


 var input = "your test string", regex = /B[a-zA-Zd]+/;
 if(!regex.test(input))
 alert('No match is found');
 else
 alert('A match is found');

Let’s try another method: match returns matches found in an array.


 var input = "your test string", 
 regex = /B[a-zA-Zd]+/g, 
 /*I've added the global modifier 'g' to the regex to get all the matches*/
 ary = input.match(regex); 
 if(ary===null)
 alert('No match is found');
 else
 alert('matches are: ' + ary.toString());

How about the chain replace? Let’s try this with regex now.

 var input = "your test string", 
 regex = /B[a-zA-Zd]+/g;
 alert(input.replace(regex, "#"));

Below is a codepen to modify. Click on the “JavaScript” tab to view the JS code.

Regex exercises

For the exercises, you can search for “regex exercises” on Google and try to solve them. Here’s what to expect when attempting these exercises, depending on the difficulty levels.

Basic

To me to be able validate a password sufficient for beginners. So, validate a password of 8 to 16 alphanumeric characters with your choice of special characters allowed.

Intermediate

This is where you should practice with more real world data and learn a few more regex points like look ahead, look behind affirmations and matching groups;

  • Validate PIN codes, hexadecimal, dates, email id, floating point.
  • Replace trailing zero, white spaces, a set of matching words
  • Extract different parts of a URL
Advanced

You can optimize the solutions of the exercises above – the most optimal regular expression for email contains thousands of characters – so go as far as you feel comfortable and that’s enough. You can also try:

  • HTML or XML parsing (although in the real world it is not recommended to do this, as using a regular expression to parse a non-regular language like HTML will never make it infallible. Additionally, XML parsing is a difficult task, more suitable for advanced users)
  • Replacing beacons
  • Removing comments (except IE conditional comments)

Regex Tools

Tools for visualize regex are one of the coolest things for me. If you ever come across a long and complex regular expression, just copy and paste them into one of these tools and you can clearly see the flow. Otherwise, there are many tools which you can use to play with the regex code. They also feature examples and cheat sheets as well as sharing features.

  • Debuggex – It draws a regex diagram according to your input and you can do a quick share with StackOverflow from there.
  • RegExr – You can test your regex with this one. It also got some references, cheat sheet, and examples to help you out.

Regex Cheat Sheet

Token Definition
[abc] Any single character a, b or c
[^abc] Any character other than a, b or c
[a-z] Character between (including) a to z
[^a-z] Character except from a to z
[A-Z] Character between (including) A to Z
. Any uniqueness
s Any space character
S Any non-blank character
re Any digit from 0 to 9
RE Any non-digit
w Any word character (letter, number and underscore)
W Any character other than a word
(…) Capture whatever is locked up
(a | b) Match a or b
a? Character a is absent or present once
a* The character a is either absent or present more times
a + The character a is present one or more times
a {3} 3 character occurrences consecutively
a {3,} 3 or more occurrences of character consecutively
a {3,6} 3 to 6 character occurrences consecutively
^ Start of chain
$ End of chain
b A word limit. If a character is the last or first character of a word or If a character is between a word or a character other than a word
B Wordless limit



Source link

Leave a Reply

%d bloggers like this: