DEV Community

Devang Tomar
Devang Tomar

Posted on • Originally published at devangtomar.hashnode.dev on

Your Ultimate Regular Expression (Regex) Cheat Sheet 👑😎

A regular expression (regex) is a pattern in input text that the regex engine attempts to match. A pattern is made up of one or more character literals, operators, or structures. It excels in searching for and manipulating text strings, as well as text file processing. Hundreds of lines of programming code can be readily replaced by a single regex line. This blog will introduce you to basic to sophisticated regex ideas that will aid you in your development work.

Why Regex? 🙋ðŸŧ

Regular expressions simplify searching and replacing processes. A frequent use case is finding a substring that matches a pattern and replacing it with something else. One of the most intriguing features is that, once youve mastered the syntax, you can use this tool in (almost) any programming language with only minor differences in the support of the most advanced features and syntax versions supported by the engines.

Characters ðŸ”Ģ

The most fundamental regex idea is the character class. It translates a small set of characters into a wider set of characters.

Groups ðŸŦ‚

A group expression is used to perform operations on all objects in a group. A group expression can be used to apply an operator to a group or to find a specific text before or after each member of the group. The parentheses are the grouping operator, and the | is used to divide the items.

Quantifiers

Quantifiers determine the number of characters or expressions to match. Quantifiers describe the number of instances of a character, group, or character class that must exist in the input for a match to be found.

Assertions 🧑ðŸŧ🏭

Boundaries, which denote the beginnings and endings of lines and words, and other patterns indicating that a match is conceivable are examples of assertions (including look-ahead, look-behind, and conditional expressions).

Flags ðŸšĐ

Flags are appended to the end of a regular expression. They are used to alter the behaviour of the regular expression.

Some useful resources ðŸŠī

Tips to increase the performance ðŸ’Ą

  • When you require parentheses but not capture, use non-capturing groups.
  • Do a quick check before attempting a match, if the regex is very complex. Ex : Does an email address contain @?
  • Present the most likely option(s) first. Ex : light green|dark green|brown|yellow|green|pink leaf
  • Minimize the amount of looping. Ex : aaaaaa+ is faster than a{6,}
  • Avoid obvious backtracking Ex : Mr|Ms|Mrs should be M(?:rs?|s)

Regex sites I use :

https://regex101.com/

https://regexone.com/

Conclusion 💭

To summarise, regex can be used to match strings of data and can be utilised in a variety of ways. Python has a regex library called re that allows you to do this. If youre on a Unix machine, you can utilise regular expressions in conjunction with grep, awk, or sed. If you wish to access all of these commands on Windows, you can utilise tools like Cygwin.

Lets connect and chat! Open to anything under the sun 🏖ðŸđ

ðŸĶ Twitter : devangtomar7

🔗 LinkedIn : devangtomar

📚 Stackoverflow : devangtomar

🖞 Instagram : be_ayushmann

Medium : Devang Tomar

Hashnode : devangtomar

🧑ðŸ’ŧ Dev.to : devangtomar

Top comments (0)