One day Mickey was waiting for a train at the station when Minnie came running to him
Minnie – Hey! Mickey
Mickey – Hi Minnie, How are you. Nice to see you here
Minnie – I am good. I need to talk to you
Mickey – Sure what is it about
Minnie – I need to know about Regex from scratch and in a way that I will always remember
Mickey – Sure, Can I come to your place in the evening, I have to catch this train now
Minnie – No Problem, I will come with you in the train and you can tell me the Story of regex, as you always do (Minnie smiling đ )
Micky – Oh-kay, Letâs go
(Mickey and Minnie boarded the train)
Mickey – Okay Minnie so Regex is this – (?<=\.) {2,}(?=[A-Z])
Minnie (angry) – Mickey, this is not funny. Get serious
Mickey – Okay, to start with Regex is short for Regular Expressions
Minnie – I know that âŚ
Mickey – Okay, letâs imagine you got a job to check all the pages of a website for double words (like âis isâ). You need to go to each and every page of the website, find double words, and highlight them
Minnie – This looks like a huge task if there are 100s of webpages
Mickey – Yes! And thatâs not all. You have to consider dots (like is. is), capitalization (is Is). Take into consideration spaces, newlines, etc.
Minnie – Oh Mickey stop. This is really getting huge and difficult
Mickey – Yes and this is a real problem
Minnie – Wait a min. Can we not create some program with the help of a programming language and do this
Mickey – Yes, you can but even there you will have to add multiple conditions, loops, etc and may not be very efficient
Minnie – Oh-kay, What do we do now
Mickey – We can do this search easily using Regular Expressions
Minnie – Oh, so Regex is used to search
Mickey – yes Right. Regular Expressions or RE or regexp or regex is a sequence of characters that define a search pattern
Minnie – I am getting this. But then why is it so popular and seems so useful and important. It just searches, Right!
Mickey – You have just seen how complicated the search condition and process may become in our example
Minnie – Yeah! I see
Mickey – Yeah
Minnie – So RegEx is a sequence of characters that forms a search pattern
Mickey – Exactly
Minnie – So how do we make the pattern for searching something
Mickey – Well we use characters, symbols, metacharacters to create a search pattern
Minnie – So can they be used in programming
Mickey – Yes Exactly you will find regular expressions being used in multiple programming languages
Minnie – Can I see some examples or rules to create a Regular Expression
Mickey – Yes, now here you go
| | A vertical bar â|â separates alternatives example gray|grey can match gray and grey both |
? | Question mark indicates zero or one occurrence of the preceding element example colou?r matches both “color” and “colourâ |
* | Asterix indicates zero or more occurrences of the preceding element example ab*c matches “ac”, “abc”, “abbc”, “abbbc”, and so on |
+ | The plus sign indicates one or more occurrences of the preceding element example ab+c matches “abc”, “abbc”, “abbbc”, and so on, but not “ac” |
Mickey – Here are some more…
\d | matches any digit |
\D | matches any non-digit character |
\w | matches any alphanumeric character |
\W | matches any non-alphanumeric character |
. | matches any character |
[a-z] | matches characters a to z |
[A-Z] | matches characters A to Z |
[0-9] | matches numbers 0 to 9 |
[abc] | matches only a, b or c |
Minnie – I am getting this slowly
Mickey – You see a regular expression can be made by combining these in a way that creates our search pattern. Like this one :Â (?<=\.) {2,}(?=[A-Z])
Minnie – Oh so do I have to remember all these rules
Mickey – It will greatly help if you can remember the basic concepts. However today you will find a lot of online applications to help create a regular expression
Minnie – Oh Great! What are they.
Mickey – You can check this
https://regexr.com/ – Learn, build and test Regular Expressions
http://buildregex.com/ – Build your regex step wise with conditions
If the above does not work check this – https://www.regextester.com/
Minnie – Oh this is Great. Now I can play and learn Regex
Mickey – Yes you can. And here comes my station. You can take the train back home
Minnie – No Mickey! I will wait for you here while I play with Regular Expressions. Will go back together in the evening
Mickey – In that case, this will be helpful for you to learn and play –
RegexOne https://regexone.com/
Minnie – This is Great. Thanks a lot Mickey, See you
Mickey – Well then, you can eat my tiffin, Here it is. Take care
Minnie – Sure Mickey and thank you. I feel so good now
Mickey – I am always here for you đ