29 days ago · Tech · 0 comments

Learn Regex the Easy Way, Part 3: Character Classes #Quick Recap # In Part 2, we learned about anchors and boundaries. The caret ^ matches the start of a line, $ matches the end, and \b matches word edges. They match positions, not characters. Now let’s match actual characters, but with more control than just typing them out literally. Square Brackets Create a Character Class # A character class is a set of characters inside square brackets: [abc]. It matches any ONE character from that set. Not all of them. Not some of them. Exactly one character from the options inside the brackets. [abc] # Match a single character: a, b, or c MATCH THESE a b c DO NOT MATCH THESE d e ab abc Notice that [abc] does NOT match “ab” or “abc.” It matches exactly one character. If your text contains “ab,” the regex will match just the “a” (the first character from the set it finds). Ranges # You can use a dash to specify a range of characters: [a-z] matches any lowercase letter [A-Z] matches any uppercase…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.