Today I worked on the Regular Expressions section (61% done so far). The challenges were all focused on using regular expressions (no surprise there!) to match strings of characters. Here are my notes from today’s session:
Quotes & Key Ideas:
- You can use a flag to match different cases of regexes. For example,
/apple/iwill match all cases of the string 'apple' such as apple, Apple, aPPle, etc.
- “You can also extract the actual matches you found with the
- “To search or extract a pattern more than once, you can use the global search flag:
- “You can have multiple flags on your regex like
- Wildcard Charcter: “The wildcard character
.will match any one character. The wildcard is also called
period. You can use the wildcard character just like any other character in the regex. For example, if you wanted to match
hum, you can use the regex
/hu./to match all four words."
- Match Single Character with Multiple Possibilities: “For example, you want to match
bog. You can create the regex
/b[aiu]g/to do this. The
[aiu]is the character class that will only match the characters
- “Inside a character set, you can define a range of characters to match using a hyphen character:
-. For example, to match lowercase letters
eyou would use
- “Using the hyphen (
-) to match a range of characters is not limited to letters. It also works to match a range of numbers. For example,
/[0-5]/matches any number between
5, including the
- It’s also possible to match both letters and numbers at the same time. For example,
/[h-s2-6]/will match the letters
sand the numbers
- Negated characters (characters you do NOT want to match): “To create a negated character set, you place a caret character (
^) after the opening bracket and before the characters you do not want to match. For example,
/[^aeiou]/gimatches all characters that are not a vowel. "
- You can use the + character to check to match a character (or group of characters) that appears one or more times in a row. “For example,
/a+/gwould find one match in abc and return
["a"]. Because of the
+, it would also find a single match in aabc and return
- While the
+sign is used to check if a character occurs one or more times, the
*sign can be used to check if a character occurs zero or more times.
Greedy vs. Lazy Matching:
- A greedy match will match the longest possible part of the string
- A lazy match will match shortest possible part that satisfies the regex expression. However, you can use the
?character to change it to lazy matching.
- “Note: Parsing HTML with regular expressions should be avoided, but pattern matching an HTML string with regular expressions is completely fine.”
- You can also use the
^carrot character to search the beginning of strings, but to search the end of a string you would use the
$dollar symbol at the end (e.g.,
/story$/will look for the string
storyat the end of a string like
This is the end of the storyand return a
shorthand character classes:
\wis used in place of
[A-Za-z0-9_]to search for alphanumeric characters.
- non-alphanumeric: To search for the opposite of alphanumeric characters (i.e,.
%, etc.) we can use
\Wwhich is equivalent to
- digits 0–9: The shortcut to look for digit characters is
\d, with a lowercase
d. This is equal to the character class
Overall I’m feeling pretty confident about today’s study session. The regular expressions haven’t been too difficult for me, though I have seen them before in my study of Python — but that was a while ago so I’m appreciating the review in these challenges.
I didn’t listen to music during today’s session — my hour of coding was broken up in to small segments at work and at home. I squeezed in the time wherever I could which worked pretty well. There’s something relaxing about working on coding challenges, it’s a nice break from the ‘day job’. Looking forward to learning more.
Link to work:
For my progress visit the timeline on my freeCodeCamp Profile.
The full log of my #100DaysOfCode journey can be viewed on GitHub.