Demystifying RegEx — 2

In my previous article Demystifying RegEx — 1 I talked about Character Set and Ranges and I want to start first solving the practice RegEx expression that I assigned you at the end of the previous article ie — /[a-zA-Z0–9][^xyz]/g.

/[a-zA-Z0–9][^xyz]/g

The above regular expression simply looks for all matches that start with a character between ‘a-z or A-z or 0–9’ followed by any character ‘except x, y, and z’. But many of you will be having confusion that why ‘r’ and ‘p’ is in the matches, that is simply because of the fact that second character can be anything except ‘x,y, and z’ and In RegEx, space, tabs(whitespaces) are also considered as a character. (It can be confusing at first but that's how RegEx works!!). Next Steps —

Repeating Characters

Repeating Characters in RegEx helps us to find matches for characters a specified number of times. Syntax — {lower-limit, upperlimit}

/[a-z]{2,5}/g

The example above — /[a-z]{2,5}/ looks for all matches that contain letters between ‘a-z’ from 2 to 5 times. 5 matches found.

/[0–9]{10}/

The example above — /[0–9]{10}/looks for a match(single match no g flag used) that contains numbers between 0–9 exactly 10 times. 1 match Only

/[0–9]{5,}/g

The example above — /[0–9]{5, }/g looks for all matches (g flag used) that contain numbers between 0–9 at least 5 times (as upper-limit is missing). 5 matches found.

Starting and Ending Characters

  • Starting Characters in RegEx helps to find whether a sequence of characters starts with a particular character. Syntax — ^ (caret)

Note — Don't confuse it with the exclude [^] character set as the ^ is present inside of the character set [] for excluding certain characters.

/^[helo][a-z]{5,}/gm

The example above — /^[helo][a-z]{5,}/gm looks for all matches that start with(^) a letter either ‘h or e or l or o’ followed by any character between ‘a-z’ at least 5 times.

Note — We have used a new flag ‘m’. ‘m’ stands for ‘multiline’ it checks for matches in different lines. without using m flag we would have found only 1 match in the first line (horticulture). Also if you have confusion about why the second ‘horticulture’ in the first line is not a match is simply because ‘^’ starts with only checks for the first letter of the sentence and not after that.

/^[A-Z][a-zA-Z0–9]{6, 12}/gm

The example above — /^[A-Z][a-zA-Z0–9]{6, 12}/gm looks for all matches (g flag) in all different lines (m flag) where the line starts with a character between ‘A-Z’ followed by any character between ‘a-z or A-Z or 0–9’ at least ‘6 to 12’ times.

  • Ending Characters in RegEx helps to find whether a sequence of characters ends with a particular character. Syntax — $ (dollar).
/[A-Za-z]{6}[0–9]$/mg

The example above — /[A-Za-z]{6}[0–9]$/mg looks for all matches in all lines that start with a character between ‘A-Z or a-z’ at least 6 times followed by a number between ‘0–9’ at the end.

Note — If by any chance any of you have doubt why hollow1 in the last line is not a match is simply because of that line ending with the word ‘match’ and not ‘hollow1'. $ only checks for the last letter of the sentence.

By now, After my first article Demystifying RegEx — 1 and this article you will be a little comfortable working with the RegEx and the Gibberish Syntax. We will continue this journey in the next article. Stay Tuned!!. As a practice try solving this-/^[A-Z][^cde]{5,8}[0–9]$/gm on your own from what you have learned until now.

Web Developer