E ExamMaster

Engineering Python · Functions & Files

Regular Expressions

In Engineering Python because lab notes hide numbers inside text — a pattern finds them without you writing a character-by-character loop.

A regular expression is a pattern that describes a family of strings. The `re` module searches text for that pattern. This lesson is search and extract — not every metacharacter in the language.

  • Engineering Python
  • Medium level
  • 4 concepts

1A pattern describes a family of strings

A pattern is not one exact string. It names a shape: "one or more digits", "a word after equals". `re` is the standard-library module that applies those shapes to text.

Write patterns as raw strings so a backslash stays a backslash in the pattern, not a Python escape.

Figure. The pattern r"lab" is a shape that fits the word lab inside the lab notes. re.search applies that shape to the text.

Literal search

import re
print(re.search(r"lab", "the lab notes"))
What is a regular expression for, in this course?
  1. Describing a family of strings to search for
  2. Compiling C++
  3. Drawing a scatter plot

re applies a pattern to text.

2search finds one; findall lists them

`re.search(pattern, text)` returns a match object for the first place the pattern fits, or None. `match.group()` is the matched text.

`re.findall(pattern, text)` returns a list of all non-overlapping matches. Use search when you need "is it there"; findall when you need every hit.

Figure. In temp=42C pressure=7, search on digit-plus returns the first run 42. findall returns both runs: 42 and 7.

Two calls

  1. searchFirst match or None. Then .group() for the text.
  2. findallA list of every match, in order.

First digit run, then all

import re
text = "temp=42C pressure=7"
m = re.search(r"\d+", text)
print(m.group())
print(re.findall(r"\d+", text))

Digits in a sensor line

text is "temp=42C pressure=7". What is the first digit run, then every digit run?

  • search(r"\d+", text).group()42
  • findall(r"\d+", text)['42', '7']

Pro tip. \d+ is one or more digits. search stops at the first run; findall keeps going.

3A few tokens you will actually type

Digit-plus matches a run of digits. Word characters are letters, digits and underscore. A dot matches one character (except a newline). Square brackets name a choice, such as a vowel class.

That is enough for labelled lab lines. Do not collect a catalogue of lookarounds and flags here — read the `re` docs when a later lab needs one extra token.

No diagram — the idea is carried by the prose, code block or coding lab.

Tokens used here
TokenMeans
digit-plusone or more digits
word charletter, digit, or _
dotone character

4Lab: pull digits from a labelled line

Search a sensor line for the first run of digits and print it.

Figure. In temp=42C, re.search for a run of digits matches 42. .group() is that matched text.

Takeaway

  1. Hold thisSearch a sensor line for the first run of digits and print it.

Coding lab. Find the reading runs in the app, with checks on your output.

Notes

  • In Engineering Python because lab notes hide numbers inside text — a pattern finds them without you writing a character-by-character loop.
  • A pattern is not one exact string. It names a shape: "one or more digits", "a word after equals". `re` is the standard-library module that applies those shapes to text.
  • `re.search(pattern, text)` returns a match object for the first place the pattern fits, or None. `match.group()` is the matched text.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

re.search finds the first match; findall lists them; a few tokens suffice.

A pattern describes a family of strings
A pattern is not one exact string. It names a shape: "one or more digits", "a word after equals". `re` is the standard-library module that applies those shapes to text.
search finds one; findall lists them
`re.search(pattern, text)` returns a match object for the first place the pattern fits, or None. `match.group()` is the matched text.
A few tokens you will actually type
Digit-plus matches a run of digits. Word characters are letters, digits and underscore. A dot matches one character (except a newline). Square brackets name a choice, such as a vow
Lab: pull digits from a labelled line
Search a sensor line for the first run of digits and print it.

Practise Regular Expressions

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 1 quick check with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.