Coding

Creating Regex Patterns | REGEX DEMYSTIFIED

  • 00:00:01 welcome to this video great to see your
  • 00:00:04 back if you watch the last video of this
  • 00:00:06 series you know why we might want to use
  • 00:00:09 regular expressions in this video we'll
  • 00:00:11 now dive into how we create patterns and
  • 00:00:15 how they actually work so let's start
  • 00:00:17 and we will start on regex 101.com a
  • 00:00:20 picture i don't own I don't get any
  • 00:00:22 money for mentioning it it's just a
  • 00:00:24 great page for working and testing and
  • 00:00:26 playing around with regular expressions
  • 00:00:28 so let's dive into that
  • 00:00:32 so as mentioned I'm on regex 101.com
  • 00:00:36 and the page is very simple to
  • 00:00:39 understand but very powerful and a great
  • 00:00:41 playing ground you have an input line
  • 00:00:43 where you can insert your regular
  • 00:00:45 expression pattern without these slashes
  • 00:00:47 so you can just start typing your
  • 00:00:49 pattern like ll and then you have the
  • 00:00:53 string below it which you want to check
  • 00:00:56 like hello there and you immediately see
  • 00:00:59 that it marks all occurrences of your
  • 00:01:02 pattern it finds now this pattern also
  • 00:01:05 has one flag enabled by default
  • 00:01:07 flags are basically just additional
  • 00:01:09 configurations you can set up on your
  • 00:01:12 patterns here it's the global flag which
  • 00:01:15 makes sure that it doesn't stop after
  • 00:01:18 finding one hit it'll match all
  • 00:01:21 occurrences of this pattern so if I have
  • 00:01:25 hello there it'll be awesome it also
  • 00:01:28 matches the seconds ii ll if I were to
  • 00:01:32 disable global only the first one was
  • 00:01:34 matched this is how it works with the
  • 00:01:38 pattern and the string and on the right
  • 00:01:40 here you even get explanation of the
  • 00:01:42 given pattern you entered it basically
  • 00:01:45 explains why it matched something and
  • 00:01:48 here it simply we explains that ll
  • 00:01:51 matches the characters ll and only these
  • 00:01:54 two characters following each other and
  • 00:01:57 in a case sensitive way then you get
  • 00:02:00 some detailed information about the
  • 00:02:01 match where it found the match and then
  • 00:02:04 at the bottom you give a quick reference
  • 00:02:05 of all the commands and special
  • 00:02:08 characters you can use in regular
  • 00:02:11 expression patterns of course we will
  • 00:02:13 walk through many of them over the next
  • 00:02:15 videos so this is a very basic match
  • 00:02:18 here ll not too exciting let's say we
  • 00:02:21 want to match the hello world at word
  • 00:02:24 and of course we can do this by simply
  • 00:02:26 entering hello there maybe don't miss
  • 00:02:29 type it now let's say we would have
  • 00:02:31 hello hello but the second hello starts
  • 00:02:35 with a lowercase character it's not
  • 00:02:37 matched because our matching strategy is
  • 00:02:39 case sensitive there are two ways of
  • 00:02:42 still getting a hit on the second hello
  • 00:02:44 – if we wanted to
  • 00:02:46 that first we could add an additional
  • 00:02:49 flag di flag for insensitive insensitive
  • 00:02:53 as the name suggests simply means it
  • 00:02:56 doesn't care for the casing you set this
  • 00:02:59 flag
  • 00:02:59 now the second hello is also matched
  • 00:03:01 because it's done in an insensitive a
  • 00:03:03 case insensitive way now let me turn
  • 00:03:06 this off though because I want to make
  • 00:03:08 this or have this work in a different
  • 00:03:11 way you can also do something else and
  • 00:03:15 create an alternative to the capital age
  • 00:03:19 you can wrap the capital H and
  • 00:03:22 parentheses to create a group basically
  • 00:03:24 and then add the pipe symbol and then
  • 00:03:28 the alternative like a lowercase H and
  • 00:03:30 now you see both strings are matched the
  • 00:03:33 H is marked as green because it's in our
  • 00:03:36 group but the match is the full word and
  • 00:03:38 you get the explanation on the right we
  • 00:03:41 have a capturing group that capturing
  • 00:03:43 part is something I'll come back to in a
  • 00:03:45 later video and then we have our two
  • 00:03:47 alternatives in a group uppercase and a
  • 00:03:49 lowercase H this already's are first a
  • 00:03:52 little more complex pattern because we
  • 00:03:56 have more flexibility there it's
  • 00:03:58 especially useful if you think about the
  • 00:04:00 alternative if you were not to use
  • 00:04:02 regular expressions if you had to check
  • 00:04:05 this with normal if conditions you would
  • 00:04:08 have to check if a given string contains
  • 00:04:11 hello with a capital H or if a given
  • 00:04:13 string contains hello with a lowercase H
  • 00:04:16 certainly doable but as you got more and
  • 00:04:19 more conditions you have to add more and
  • 00:04:21 more if statements whereas here you just
  • 00:04:24 have to add more and more logic to your
  • 00:04:26 pattern which is definitely an
  • 00:04:28 improvement and makes it much easier the
  • 00:04:30 more complex your pattern grows and you
  • 00:04:33 start to feel the effects very early now
  • 00:04:36 let's say when I would take it even
  • 00:04:37 further and we also wanted to match ello
  • 00:04:41 now clearly it's not matched right now
  • 00:04:44 because we match hello with a lowercase
  • 00:04:46 or uppercase H so what we would need to
  • 00:04:50 do to also match ello is we would have
  • 00:04:53 to make something optional D H now we do
  • 00:04:57 this by adding a quest
  • 00:04:58 mark after our group this simply means
  • 00:05:01 the part immediately prior to the
  • 00:05:04 question mark is optional so the Apple
  • 00:05:09 case or lowercase H isn't the end
  • 00:05:11 optional it doesn't matter
  • 00:05:13 that's why ello is also matched now
  • 00:05:17 important if I were to add the question
  • 00:05:19 mark after de ello is not matched so the
  • 00:05:23 question mark does not mean everything
  • 00:05:25 in front of it is optional just the
  • 00:05:28 direct character in front of it so in
  • 00:05:32 this case only the e would be optional
  • 00:05:33 if you want to make everything in front
  • 00:05:36 of it optional you would have to group
  • 00:05:38 it again
  • 00:05:39 so now LLO is also matched but that's of
  • 00:05:42 course not the goal here
  • 00:05:43 I want you make the H optional this is
  • 00:05:47 how we build these patterns we think
  • 00:05:50 about what we want to achieve and then
  • 00:05:52 we got a rich feature set of rules we
  • 00:05:54 can pick off or pick from to construct
  • 00:05:57 this now we only started with groups
  • 00:06:00 alternatives and optional characters we
  • 00:06:03 already got some nice tools though now
  • 00:06:05 in the next video I also want to dive
  • 00:06:07 into how we can match for words if a
  • 00:06:11 part of the word fulfills a certain
  • 00:06:13 criteria and how we can add more cool
  • 00:06:16 rules to get really powerful matchings
  • 00:06:19 and checks with regular expressions
  • 00:06:21 can't wait to see you there bye