liveBook · Manning

Regular expression

As explained in Deep Learning with Python, Third Edition

A pattern language for matching and manipulating character sequences in text.

Regular expressions match character patterns to split text into tokens, making them a practical first step before converting language into numeric inputs. section 14.2

From a sentence to tokens

Use "." to match each character, or combine "[\\w]+" with "[.,!?;]" to capture words and punctuation separately. Applied to "The quick brown fox jumped over the dog.", the latter produces tokens such as "The", "quick", "brown", and ".". section 14.2

Practical rules:

  • Choose character tokens when individual symbols matter. section 14.2
  • Choose word-and-punctuation patterns when preserving readable text units matters. section 14.2
  • Index the resulting tokens separately; regex performs splitting, not numeric encoding. section 14.2
def split_words(text):
    return re.findall(r"[\w]+|[.,!?;]", text)

Inspect the compact pattern that separates word runs from punctuation. section 14.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains matching patterns through working splitting examples, then stops before detailed regex syntax or engine internals.
Examples
Uses character matching, word-and-punctuation matching, and the sentence “The quick brown fox jumped over the dog.”
What you can do after
Apply via a regex library to split strings into character or word tokens for subsequent numeric indexing.
How it connects
Connects regular expressions to tokenization, text preprocessing, numeric indexing, ASCII, and Unicode.

Also explained in

Compare how 27 books explain regular expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.