Experience regular expressions (regex), a ground-breaking search pattern language to rapidly discover the content you’re searching for.
Image credits: xkcd.com
While registering an account for social media application or finishing a request for a online order, each piece of data you enter into a web form is approved. Did you enter an email including a @ symbol? Did you enter a telephone number 10 digits in length, with or without -s and parentheses? And afterward there’s the immortal of all, did your new secret key meet the prerequisites for inclusion (and exclusion) of symbols, digits, and both upper and lower case letters?
While rectifying each field in our digital lives for proper format can be a hell of a headache, it’s guaranteeing that our records are secure, our packages are effectively delivered, and that we can be reached by telephone and email.
1. Knowledge graphs and Chatbots — An analytical approach.
2. 🤖 How to talk to Computers: A Framework for building Conversational Agents — Part 1
The innovation that energizes this check framework on almost every website and application is the regular expressions, regularly abbreviated to regex or regexp. A regular expression is an arrangement of characters that describe an example of content that ought to be found, or matched, in a string you enter. By matching text, we can distinguish how frequently and where certain piece of text occur, just as to have the chance to replace or refresh the text of content if necessary.
Regular Expressions have a variety of use cases including:
- validating user input in HTML forms
- verifying and parsing text in files, code and applications
- examining test results
- finding keywords in emails and web pages
While there are a variety of implementations of Regular Expressions across platforms, in this lesson you will learn the basics that apply everywhere. By the lesson’s end, you’ll be empowered to use them in your own projects and become a bahubali of regex!
Do you feel the need of regular expression superpowers running through your body?
Regular expressions are special sequences of characters that describe a pattern of text that is to be matched
- We can use literals to match the exact characters that we need. The regex
9
will match the9
in the piece of text139
, and the regex5 choclates
will completely match the text5 choclates
! - Alternation allows us to match the text preceding or following the pipe symbol
|
. The regexburger|pizza
will matchburger
in the textI love burger
, but will also matchpizza
in the textI love pizza
. - Character sets, denoted by a pair of brackets
[]
, let us match one character from a series of characters. The regex[pasta]
will match the charactersp
,a
,s
,t
ora
but not the textpasta
. - Wildcards, represented by the period or dot
.
, will match any single character (letter, number, symbol or whitespace). The regex.....
will completely matchpizza
andpasta
! Similarly, the regexI ate . pizzas
will completely match bothI ate 3 pizzas
andI ate 8 pizzas
! - Ranges allow us to specify a range of characters in which we can make a match. The regex
I saw[2-9] [b-h]ats
will match the textI saw 4 bats
as well asI saw 8 cats
and evenI saw 5 hats
. - Shorthand character classes like
w
,d
ands
represent the ranges representing word characters, digit characters, and whitespace characters, respectively. The regexdswwwwww
will matches a digit character, followed by a whitespace character, followed by 6 word characters. Thus the regex completely matches the text3 pizzas
. - Groupings, denoted with parentheses
()
, group parts of a regular expression together, and allows us to limit alternation to part of a regex. The regexI love (burger|pizza)
will match the textI love
and then match eitherburger
orpizza
, as the grouping limits the reach of the|
to the text within the parentheses. - Fixed quantifiers, represented with curly braces
{}
, let us indicate the exact quantity or a range of quantity of a character we wish to match. The regexpizza{3}s
will match the characterspizz
followed by3
a
s, and then the characters
, such as in the textpizzaaas
. The regexpizza{1,7}s
will match the characterspizz
followed by at least1
a
s and at most7
a
s, followed by ans
, matching the stringspizzas
,roaaaaar
androaaaaaaar
. - Optional quantifiers, indicated by the question mark
?
, allow us to indicate a character in a regex is optional, or can appear either0
times or1
time. The regexThe ate a (fabulous)? pizza
will completely match bothThe ate a fabulous pizza
andThe ate a pizza
. - The Kleene star, denoted with the asterisk
*
, is a quantifier that matches the preceding character0
or more times. The regexmeo*w
will match the charactersme
, followed by0
or moreo
s, followed by aw
. Thus the regex will matchmew
,meow
,meooow
, andmeoooooooooooow
. - The Kleene plus, denoted by the plus
+
, matches the preceding character1
or more times. The regexmeo+w
will match the charactersme
, followed by1
or moreo
s, followed by aw
. Thus the regex will matchmeow
,meooow
, andmeoooooooooooow
, but not matchmew
. - The Anchor symbols hat
^
and dollar sign$
are used to match text at the start and end of a string, respectively. The regex^The pizza is fabulous$
will completely match the textThe pizza is fabulous
but not matchThe pizza is fabulous at uncles home
orThe pizza is fabulous at the party
. The^
ensures that the matched text begins withThe
, and the$
ensures the matched text ends withfabulous
.
Example:
Regex expression: ^([w-.]+)@(([([0–9]{1,3}.){3}[0–9]{1,3}])|(([w-]+.)+)([a-zA-Z]{2,4}))$
Matches: vinay@somewhere.com | vinay.kumar@[1.1.1.1] | vinay@a.b.c.d.info
Do you just want to scream ah+
really loud? Awesome! You are now ready to take these skills and use them out in the wild.😉😉