Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Regex Syntax and Basic Patterns | Introduction to Regular Expressions
Python Regular Expressions

bookRegex Syntax and Basic Patterns

Regex Syntax: Building Blocks

Regular expressions, or regex, are constructed using a combination of literals, metacharacters, and character classes. Understanding these components is essential for writing effective patterns.

  • Literals are ordinary characters that match themselves. For example, the pattern "cat" matches the exact string "cat";
  • Metacharacters are special symbols that have unique meanings in regex. The most common metacharacters include:
    • . (dot): Matches any single character except a newline;
    • ^: Matches the start of a string;
    • $: Matches the end of a string;
    • *: Matches zero or more occurrences of the preceding element;
    • +: Matches one or more occurrences of the preceding element;
    • ?: Matches zero or one occurrence of the preceding element.
  • Character classes allow you to match any one character from a set of characters. For example, [abc] matches "a", "b", or "c", and [0-9] matches any digit from 0 to 9.
123456789101112131415
import re text = "User123, admin42, guestX" # Find all digits digits = re.findall(r"[0-9]", text) print("Digits found:", digits) # Find all lowercase letters lowercase_letters = re.findall(r"[a-z]", text) print("Lowercase letters found:", lowercase_letters) # Find all uppercase letters uppercase_letters = re.findall(r"[A-Z]", text) print("Uppercase letters found:", uppercase_letters)
copy

In this example, you use the re.findall() function to search for matches in the string "User123, admin42, guestX". The pattern [0-9] matches any digit between 0 and 9, so it finds all individual digits in the text. The pattern [a-z] matches any lowercase letter from "a" to "z", and [A-Z] matches any uppercase letter from "A" to "Z". Each character class is enclosed in square brackets, specifying the range or set of characters to match. These patterns are fundamental in extracting specific types of characters from text.

question mark

What does the regex pattern [a-z] match?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 6.67

bookRegex Syntax and Basic Patterns

Sveip for å vise menyen

Regex Syntax: Building Blocks

Regular expressions, or regex, are constructed using a combination of literals, metacharacters, and character classes. Understanding these components is essential for writing effective patterns.

  • Literals are ordinary characters that match themselves. For example, the pattern "cat" matches the exact string "cat";
  • Metacharacters are special symbols that have unique meanings in regex. The most common metacharacters include:
    • . (dot): Matches any single character except a newline;
    • ^: Matches the start of a string;
    • $: Matches the end of a string;
    • *: Matches zero or more occurrences of the preceding element;
    • +: Matches one or more occurrences of the preceding element;
    • ?: Matches zero or one occurrence of the preceding element.
  • Character classes allow you to match any one character from a set of characters. For example, [abc] matches "a", "b", or "c", and [0-9] matches any digit from 0 to 9.
123456789101112131415
import re text = "User123, admin42, guestX" # Find all digits digits = re.findall(r"[0-9]", text) print("Digits found:", digits) # Find all lowercase letters lowercase_letters = re.findall(r"[a-z]", text) print("Lowercase letters found:", lowercase_letters) # Find all uppercase letters uppercase_letters = re.findall(r"[A-Z]", text) print("Uppercase letters found:", uppercase_letters)
copy

In this example, you use the re.findall() function to search for matches in the string "User123, admin42, guestX". The pattern [0-9] matches any digit between 0 and 9, so it finds all individual digits in the text. The pattern [a-z] matches any lowercase letter from "a" to "z", and [A-Z] matches any uppercase letter from "A" to "Z". Each character class is enclosed in square brackets, specifying the range or set of characters to match. These patterns are fundamental in extracting specific types of characters from text.

question mark

What does the regex pattern [a-z] match?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 2
some-alt