Susanna Learns Regex Part I
Susanna Learns Regex Part I
ARRRRGH so don't ask me to explain what regex actually is; okay fine it stands for regular expressions; at the end of a long and hard workday I finally got down to doing some regex. Regex is basically a way of matching strings I guess.
Or other things.
It's basically a tool for pattern matching.
Literals
A literal in regex is when you literally match something precisely.
It doesn't have to be the full thing; it can just be a part of something; but it does have to match it exactly.
bananas will match with bananas
usanna will match with susanna
monkey will match with i am a monkey and i litearlly like to climb all over things in my house and i literally like to eat bananas... not saying if any of that is true... or not...
Alternation
Alternation in regex allows us to match for multiple things.
You can use a pipe delimiter to match for two or more things.
alan turning|maragaret elaine hamilton
will allow us to match for both
i love alan turing
and
i love margaret elaine hamilton
Character Sets
Arrrgh, these are a little bit more complicated! These allow you to denote for multiple potential characters or options at once.
Let's check for all the potential spellings of Susanna:
su[sz]anna?
? indicates for optionality!
Wildcards
"Sometimes we don't actually care WHAT characters are in a text, just that there are SOME characters!". Codecademy said it best (I am doing their Regex course, I think it's free).
. just means a character
Filtering only for seven-letter names? ....... will match susanna and alferdo and aurelia and alberto alike!
I am .. years old will match I am 29 years old and I am 30 years old... yep this is a thing rn.
What if we want a full stop? We use an escape character \.
I am .. years old\. will make sure that we end my new age with a full stop! I am 30 years old. This has been a major life change for me...
Comments
Post a Comment