Learn to write Regular Expressions in minutes (Day 5)

Hi, are you ready to learn basics of regular expressions? All we need to do is memorize a set of rules.

to test each rule, you could use any tool that supports regex search. Here's one. And another one.

  • the simplest rule is to search a sequence of characters. We type the search string in between two slashes. for example, to search for fox within the string The quick brown fox jumps over the lazy dog!, just write /fox/. Try these as well: /Fox/,/t/, /!/, / /
  • regex is case sensitive, right? What if we want to consider both uppercase and lowercase? disjunctions will help us to choose between multiple options. Try /[Tt]he/ for the above string. Hmm.. This might help us if we wanted to say something like "any of the alphabets", or "any of the digits". right? Try /[abcdefghtjklmnopqrstuvwxyz]/ or /[0123456789]/. I know its painful to type in these. Does it make you happy if you could specify the range to select, instead? Let's test it right now. Try /a-z/ or /0-9/.
  • what if we want to say "find string that does not include this". You could use ^ character for that. Try, /[^T]he/. Which is similar to this /[t]he/ right? What happens if we write /[^a-z]/?
  • suppose we encounter a situation, where we wanted to consider both color and colour, where u is optional. We could use a ? to specify the previous character as optional. Try /colou?r/ over the string color and colour are the same.
  • suppose you have a pet sheep. You need to highlight whatever it says. It might say, baa, baaa, baaaa, ... What will you do? If you could select any string starting with b followed by any number of as, will work. * will help you on that. Try /ba*/. * will consider any repetition of character that precedes it. Try /b[aA]*/ over baA,baaAa, bAaaaA as well. You could use + as well instead of *.
  • if you want to match any character, that's not a newline, use .. Try /./ over The quick brown fox jumps over the lazy dog!. Lazy! So, what if you want to specify a . as a . character? You need to specify it along with a \. Try /\./ over A long pause ....
  • What if you want to select both color and colir, where any character should be allowed between l and r? Try /col.r/ over colir color colwr collr colour.
  • you run a pet store. You want to select if a cat or a dog appears in a document you are editing. /[cat dog]/ wouldn't work for sure. The solution is to use a disjunction symbol |. Try /[cat dog]/, /cat|dog/ and /cats?|dogs?/ over cats love dogs.
  • again in the pet store, what if you want to select both puppy and puppies? () will help you in this. Any expression inside the () will be considered as a single character in the outside expression. Try /pupp(y|ies)/ over puppy or puppies.

So far we've discussed the rules to select specific patterns. The regex could also help us to substitute values for a subsection of a string. Capture groups will help here. Suppose we want to match a sentence the faster we run, the faster they run but not the faster we run, the faster they eat. As you can see, the sentence has a repeating pattern. So, we could substitute similar parts of those sentences at multiple positions. We could group each of those substitutes within parenthesis. And substitute them based on their order. Try this regex. The (.*)er they (.*), the \1er we \2 over the faster we run, the faster they run but not the faster we run, the faster they eat. As you observe, the substitution happens in the order of capture groups. What if we don't want to include a group? We could use a non-capturing group, like this (?:) instead of ().

Congrats! You've learned the basics of writing regular expressions. I know these examples are not enough to learn it well. But its just enough to design our chatbot. Next day, we will be designing a chatbot named Eliza. Eliza was the first chatbot capable of passing Turing test. Meanwhile, just play with regular expressions and be ready. Till then.

About Author

Jubin Jose

Chatbot Developer at Cedex Technologies LLP | Technology: C, Node JS Hobby: beginner ML & AI + creative coding + Youtuber.

Want to work with us?

Be free to contact us for any of your chatbot development queries. As a company specialized in Chatbot development, we can provide you quality services. We will be happy to provide you a free quote.

Contact Us