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)