avatarTomas Svojanovsky

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1417

Abstract

the beginning.</p><h2 id="c597">Meta characters</h2><p id="fc03">We need these meta tags to make a complex pattern, which helps us easily make a blueprint for the string we need to match.</p><ul><li>. Any character (except newline character) <code>hu..n</code></li><li>^ Starts with <code>^hello</code></li><li> Ends with <code>world</code></li><li>* Zero or more occurrences <code>py*</code></li><li>+ One or more occurrences <code>py+</code></li><li>{ } Exactly the specified number of occurrences <code>py{2}</code></li><li>[] A set of characters <code>[a-z]</code></li><li>\ Signals a special sequence (can also be used to escape special characters) <code>\d</code></li><li>| Either or <code>python|javascript</code></li><li>( ) Capture and group</li></ul><h2 id="07e9">Quantifiers</h2><p id="b825">We covered some of these in the meta characters. Quantifiers are simply a way of specifying how many times a pattern should be matched.</p><p id="d37c">This is particularly useful, for instance, when allowing variations in formatting, such as typing a phone number with or without whitespace, like <code>+420 111222333</code> or <code>+420111222333</code>.</p><ul><li>* : 0 or more</li><li>+ : 1 or more</li><li>? : 0 or 1, used when a character can be optional</li><li>{4} : exact number</li><li>{4,6} : range numbers (min, max)</li></ul><p id="38fa">We have everything we need to see some examples in action, wh

Options

ich we will do in the next part. Of course, there is much more to learn about regexes, but it doesn’t make sense to try to learn everything at once. We might forget half of it, and we won’t use the other half.</p><p id="6c38">It’s better to start small, understand the basics, and then move on to more complex patterns. This is how we will continue in the <a href="https://tomas-svojanovsky.medium.com/regexes-for-normal-people-like-me-in-python-2-3-47a69e35c805">next part.</a></p><p id="25a1">If you enjoyed the read and want to be part of our growing community, hit the follow button, and let’s embark on a knowledge journey together.</p><p id="f729">Your feedback and comments are always welcome, so don’t hold back!</p><h1 id="5555">Stackademic</h1><p id="b835">Thank you for reading until the end. Before you go:</p><ul><li>Please consider <b>clapping</b> and <b>following</b> the writer! 👏</li><li>Follow us <a href="https://twitter.com/stackademichq"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/stackademic">LinkedIn</a> | <a href="https://www.youtube.com/c/stackademic">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a></b></li><li>Visit our other platforms: <a href="https://plainenglish.io/"><b>In Plain English</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></li></ul></article></body>

Regexes for Normal People (Like Me) in Python (1/3)

Part 1: Understanding the Basics of Regular Expressions in Python

Regexes are hard and boring? Not if you know the rules. I had a colleague in my early job and I used to ask myself: how did he know what to do with regexes — they are super hard. Well, now I know.

First thing first…

In Python, we need the re module to start working with regexes. We can use re.compile() or directly call re with some methods. Which methods? Let's discover them.

  • match(): Determine if the RE matches at the beginning of the string.
  • search(): Scan through a string, looking for any location where this RE matches.
  • findall(): Find all substrings where the RE matches, and returns them as a list.
  • finditer(): Find all substrings where the RE matches, and returns them as an iterator.

I find search and findall to be the most useful methods. match and finditer are more suitable for special cases. We don't always need to work with iterators, and match is specifically for searching from the beginning.

Meta characters

We need these meta tags to make a complex pattern, which helps us easily make a blueprint for the string we need to match.

  • . Any character (except newline character) hu..n
  • ^ Starts with ^hello
  • $ Ends with world$
  • * Zero or more occurrences py*
  • + One or more occurrences py+
  • { } Exactly the specified number of occurrences py{2}
  • [] A set of characters [a-z]
  • \ Signals a special sequence (can also be used to escape special characters) \d
  • | Either or python|javascript
  • ( ) Capture and group

Quantifiers

We covered some of these in the meta characters. Quantifiers are simply a way of specifying how many times a pattern should be matched.

This is particularly useful, for instance, when allowing variations in formatting, such as typing a phone number with or without whitespace, like +420 111222333 or +420111222333.

  • * : 0 or more
  • + : 1 or more
  • ? : 0 or 1, used when a character can be optional
  • {4} : exact number
  • {4,6} : range numbers (min, max)

We have everything we need to see some examples in action, which we will do in the next part. Of course, there is much more to learn about regexes, but it doesn’t make sense to try to learn everything at once. We might forget half of it, and we won’t use the other half.

It’s better to start small, understand the basics, and then move on to more complex patterns. This is how we will continue in the next part.

If you enjoyed the read and want to be part of our growing community, hit the follow button, and let’s embark on a knowledge journey together.

Your feedback and comments are always welcome, so don’t hold back!

Stackademic

Thank you for reading until the end. Before you go:

Python
Regex
Programming
Software Development
Software Engineering
Recommended from ReadMedium