More and more people are getting interested in Python, and for beginners—or for developers coming from another language—the first steps can feel a little unclear. When the goal is big, it often helps to begin with something small and practical. Writing a simple Python scraper is one good place to start, and regular expressions are one of the basic tools behind it.

A simple way to begin with Python regular expressions

The starting point is straightforward:

import re

That single import brings in Python’s built-in support for regular expressions.

Using the re module

A common pattern is to compile a regular expression first and then use it for matching:

#codeing=utf-8
import re
p = re.compile(r'17python')#创建Pattern对象
m = p.match('17python.com')
if m :
    print(m.group())
# 输出结果为:17python

In this example, re.compile() creates a Pattern object. Then match() checks from the beginning of the string. Since 17python.com starts with 17python, the match succeeds, and group() returns the matched text.

There is also a shorter way to do the same thing by calling a function from re directly instead of using a compiled pattern object:

#使用re模块方法代替实例方法
print(re.match('17python', '17python.com').group())
# 输出结果为:17python

Both approaches work. In simple cases, the direct call is convenient; when the same pattern will be used repeatedly, compiling it first is often clearer.

Common re methods you will use often

When scraping or extracting text, a few methods appear again and again. The following example shows several of the most common ones:

p = re.compile(r'17python')
s1 = '17python.com'
s2 = 'www.17python.com'
s3 = '17python.com17python.com'
s4 = 'abc.com'
p4 = re.compile('abc')
print(p.match(s1).group())
print(p.search(s2).group())
print(p.findall(s3))
print(p4.sub('17python', s4))

These calls demonstrate different kinds of text handling:

  • match() checks whether the pattern appears at the start of the string.
  • search() looks through the string and returns the first match it finds.
  • findall() returns all matching results.
  • sub() replaces matched content with new text.

These are some of the most frequently used tools in Python’s re module, and they are especially useful once you start collecting and cleaning data.

Common metacharacters in regular expressions

To write effective patterns, you need to know the basic metacharacters and what they mean:

  • . matches any character except a newline
  • ^ matches the start position; in multiline mode it matches the start of each line
  • $ matches the end position; in multiline mode it matches the end of each line
  • * matches the previous metacharacter 0 or more times
  • + matches the previous metacharacter 1 or more times
  • ? matches the previous metacharacter 0 or 1 time
  • {m,n} matches the previous metacharacter from m to n times
  • \ is the escape character; the character after it loses its special meaning, so for example \. matches a literal . instead of any character
  • [] defines a character set and matches any one character inside it
  • | means logical OR, so a|b matches a or b
  • (…) creates a group; by default it is capturing, and captured content can be retrieved separately. Group indexes start from 1 and follow the order of (
  • (?iLmsux) sets modes within a group; each character in iLmsux represents a mode
  • (?:…) is a non-capturing group and is skipped when indexes are assigned
  • (?P…) is a named group; its content can be retrieved by index or by name
  • (?P=name) references a previously named group inside the same regular expression
  • (?#…) is a comment and does not affect the rest of the expression
  • (?=…) is positive lookahead, meaning the text to the right must match the pattern inside the parentheses
  • (?!…) is negative lookahead, meaning the text to the right must not match the pattern inside the parentheses
  • (?<=…) is positive lookbehind, meaning the text to the left must match the pattern inside the parentheses
  • (?<!…) is negative lookbehind, meaning the text to the left must not match the pattern inside the parentheses
  • (?(id/name)yes|no) applies the yes pattern if the specified group id or name matched earlier; otherwise it applies the no pattern
  • \number matches the same text captured by the earlier group with that index number
  • \A matches the start of the string, ignoring multiline mode
  • \Z matches the end of the string, ignoring multiline mode
  • \b matches an empty string at the beginning or end of a word
  • \B matches an empty string that is not at the beginning or end of a word
  • \d matches a digit, equivalent to [0-9]
  • \D matches a non-digit, equivalent to [^0-9]
  • \s matches any whitespace character, equivalent to [ \t\n\r\f\v]
  • \S matches any non-whitespace character, equivalent to [^ \t\n\r\f\v]
  • \w matches any digit, letter, or underscore, equivalent to [a-zA-Z0-9_]
  • \W matches any character that is not a digit, letter, or underscore, equivalent to [^a-zA-Z0-9_]

If you want to go deeper

Once these basics feel familiar, it helps to continue with more detailed material on Python regular expressions, such as:

  • a Python regular expression guide
  • a detailed explanation of Python regular expressions
  • the official Python re module documentation

For a beginner building a small scraper, this is already enough to get started: import re, write a pattern, test it with match() or search(), then use findall() and sub() when you need extraction or replacement.