Regex commands for beginners
2 minute read
Regex commands for beginners
Regular expressions, also known as regex, are a powerful tool for working with text. They allow you to search for, match, and manipulate specific patterns of characters within a body of text.
Here are a few basic regex commands that every beginner should know:
^- Matches the start of a line. For example, the regex^Hellowould match the string “Hello” at the beginning of a line.$- Matches the end of a line. For example, the regexWorld$would match the string “World” at the end of a line..- Matches any single character (except a newline). For example, the regexH.llowould match the strings “Hello”, “Hillo”, and “Hxllo”, but not “Hollo”.**- Matches zero or more occurrences of the preceding character or group. For example, the regex “H*llo” would match the strings “Hello”, “Hllo”, and “Hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhllo”.+- The plus symbol (+) matches one or more occurrences of the preceding character or group. For example, the regexH+llowould match the strings “Hello” and “Hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhllo”, but not “Hllo”.?- The question mark (?) matches zero or one occurrence of the preceding character or group. For example, the regexH?llowould match the strings “Hello” and “Hllo”, but not “Hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhllo”.[ ]- Brackets enclose a character set, which matches any single character within the set. For example, the regexH[ae]llowould match the strings “Hello” and “Hallo”, but not “Hxllo”.[^ ]- When the caret symbol is placed within brackets, it negates the character set. For example, the regexH[^ae]llowould match the string “Hxllo”, but not “Hello” or “Hallo”.-
|- The pipe symbol () represents a logical OR. For example, the regex H(e|a)llowould match the strings “Hello” and “Hallo”. \d- The backslash and “d” combination matches any single digit. For example, the regex\d\d\d-\d\d-\d\d\d\dwould match a string in the format “XXX-XX-XXXX”, where “X” is a digit.
These are just a few basic regex commands that can help you get started working with regular expressions. With practice and a bit of creativity, you can use these commands to perform a wide range of tasks, such as searching for and replacing specific patterns of text within a document, validating forms, and more.
Let me know what you think of this article in the comment section below!
comments powered by Disqus