David's Blog

How to use regular expressions in Rust

By David Li on Fri, 3 January 2024

Regular expressions are a powerful tool in text processing, and Rust provides a robust library for working with them. In this article, we will explore the basics of using regular expressions in Rust, including syntax, matching, and capturing.

Syntax

Rust’s regular expression syntax is based on the Perl-compatible regular expression (PCRE) syntax. This means that many of the common regular expression constructs, such as character classes and quantifiers, are the same as in Perl.

To use regular expressions in Rust, you first need to import the regex crate:

use regex::Regex;

Once you have imported the Regex type, you can create a regular expression pattern by calling the Regex::new method:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();

In this example, we are creating a regular expression pattern that matches the string “hello, world!” or “hello, universe!“. The r before the string literal indicates that this is a raw string, which allows us to use backslashes to escape characters without having to double them up.

Matching

To test whether a string matches a regular expression pattern, you can use the Regex::is_match method:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();
let input = "hello, world!";
let is_match = pattern.is_match(input);

In this example, is_match will be true because the input string matches the regular expression pattern.

If you want to find the first match of a regular expression pattern in a string, you can use the Regex::find method:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();
let input = "hello, world!";
let first_match = pattern.find(input);

In this example, first_match will be an Option<Match> type, which represents the first match of the regular expression pattern in the input string. If there is no match, first_match will be None.

If you want to find all matches of a regular expression pattern in a string, you can use the Regex::find_iter method:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();
let input = "hello, world! hello, universe!";
let matches: Vec<_> = pattern.find_iter(input).collect();

In this example, matches will be a Vec<Match> type, which contains all matches of the regular expression pattern in the input string.

Capturing

Regular expressions can also be used to capture parts of a matched string. To do this, you can use parentheses to group the parts you want to capture:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();
let input = "hello, world!";
let first_match = pattern.captures(input);

In this example, first_match will be an Option<Captures> type, which represents the captured groups of the regular expression pattern in the input string. If there is no match, first_match will be None.

You can access the captured groups by using the Captures::get method:

let pattern = Regex::new(r"hello, (world|universe)!").unwrap();
let input = "hello, world!";
let first_match = pattern.captures(input);

if let Some(captures) = first_match {
    let captured_group = captures.get(1).unwrap().as_str();
    println!("Captured group: {}", captured_group);
}

In this example, captured_group will be the string “world”, which is the first captured group in the regular expression pattern.

Conclusion

Regular expressions are a powerful tool for text processing, and Rust’s regex crate provides a robust library for working with them. By mastering the basics of regular expression syntax, matching, and capturing, you can harness the full power of regular expressions in your Rust applications.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-25.