python-discord/meta

Tag suggestion: regex

camcaswell opened this issue · 2 comments

Tag Name

regex

Motivation

Beginners asking how to parse input or find something in a string are often told that the solution is "regex", which they usually have never heard of. It can be hard for them to get their bearings after being whisked from a topic that they're comfortable with, inputs and strings, to a language embedded within python with its own module and opaque syntax. This tag should give them perspective on what regex is and point them to resources if they decide they want to learn how to use it.

Content draft

Regular expressions
Sometimes you want to detect a certain pattern of characters in a string without enumerating every possibility. Regular expressions (regex) are a language for specifying those patterns that is used in many programming languages. Python's standard library includes the re module, which defines functions for using regex patterns.

Example
We can use regex to pull out all the numbers in a sentence:

>>> import re
>>> x = "On Oct 18, 1963 Félicette the cat was launched into space aboard Veronique AGI sounding rocket No. 47."
>>> pattern = r"[0-9]{1,4}"
>>> re.findall(pattern,  x)
['18', '1963', '47']

You can read about the regex syntax and the functions that use regex patterns here.
regex101.com is a great site for experimenting with regular expressions.

The example might be more impactful if you include # Matches one to four numbers on the third line, and define x with a substring that matches \d{5,} to illustrate the constraint that {1,4} imposes.

The reason I didn't do something like that is that then you either get a partial match of the number or you have to make the pattern more complex. I think both are more confusing than they're worth since the tag isn't meant to teach them regex syntax, just introduce the concept.

Between the two I'd rather show the partial matches since, together with your suggested comment, they might be able to figure out what's going on.