Movatterモバイル変換


[0]ホーム

URL:


Skip to contents

re: Regular Expressions Made Easy in R

Overview

re is an R package designed to simplify working with regular expressions by providing a set of functions similar to Python’sre module. The package includes utilities for compiling regular expressions with specific flags, checking for matches, escaping special characters, and more. By emulating the functionality and naming conventions of Python’sre module,re aims to make regex operations in R more intuitive and accessible, especially for those familiar with Python.

Installation

You can install there package directly from GitHub.

# Install devtools if you haven't alreadyinstall.packages("devtools")# Install re package from GitHubdevtools::install_github("pythonicr/re")

Getting Started

Basic Usage

Here are some examples demonstrating how to use the functions provided by there package.

Compile a Regular Expression with Specific Flags

There_compile function compiles a regular expression pattern with specified flags. This step is optional as flags and patterns can be provided for any of the functions.

library(re)# Compile a pattern with case-insensitive matchingpattern<-re_compile("^abc", IGNORECASE=TRUE)# Compile a pattern with multi-line matching (abbreviations are based on Python's re package)pattern<-re_compile("end$", M=TRUE)pattern<-re_compile("end$", MULTILINE=TRUE)# Compile a pattern with DOTALL flagpattern<-re_compile("a.b", DOTALL=TRUE)

Check if a String Contains a Regular Expression

There_contains function checks whether a specified pattern is found within each element of a character vector.

# Check if strings contain a patternre_contains(pattern,"Abcdef")#> [1] FALSEre_contains("xyz$","hello world xyz")#> [1] TRUE

Escape Special Characters

There_escape function escapes all special characters in a regular expression string.

# Escape special characters in a stringescaped_pattern<-re_escape("a[bc].*d?")print(escaped_pattern)#> [1] "a\\[bc\\]\\.\\*d\\?"

Extract All Occurrences of a Pattern in a String

There_findall function extracts all occurrences of a specified pattern from each element of a character vector.

# Extract all words from a stringpattern<-re_compile("\\b\\w+\\b")re_findall(pattern,"This is a test.")#> [[1]]#> [1] "This" "is"   "a"    "test"re_findall("\\d+","123 abc 456")#> [[1]]#> [1] "123" "456"

Match a Pattern Against the Entire String

There_fullmatch function checks whether each element of a character vector fully matches a specified pattern.

# Check for full matches in a stringpattern<-re_compile("\\d{3}-\\d{2}-\\d{4}")re_fullmatch(pattern,"123-45-6789")#> [[1]]#> [1] "123-45-6789"re_fullmatch("123-45-6789","123-45-6789 and more")#> [[1]]#> [1] NA

Match a Pattern at the Start of a String

There_match function checks whether each element of a character vector matches a specified pattern at the start.

# Check for matches at the start of a stringpattern<-re_compile("\\d{3}")re_match(pattern,"123abc")#> [[1]]#> [1] "123"re_match("abc","xyzabc")#> [[1]]#> [1] NA

Search for a Pattern in a String

There_search function searches for occurrences of a specified pattern within each element of a character vector.

# Search for a pattern in a stringpattern<-re_compile("\\d+")re_search(pattern,"abc 123 xyz")#> [[1]]#> [1] "123"re_search("\\bword\\b","A sentence with the word.")#> [[1]]#> [1] "word"

Split a String by a Regular Expression Pattern

There_split function splits each element of a character vector into substrings based on a specified pattern.

# Split strings based on a patternpattern<-re_compile("\\s+")re_split(pattern,"Split this string")#> [[1]]#> [1] "Split"  "this"   "string"re_split("\\W+","Split,with!punctuation.morestuff", maxsplit=2)#> [[1]]#> [1] "Split"                 "with"                  "punctuation.morestuff"

Substitute Occurrences of a Pattern in a String

There_sub function replaces all occurrences of a specified pattern in each element of a character vector with a replacement string.

# Substitute patterns in a stringpattern<-re_compile("\\d+")re_sub(pattern,"number","Replace 123 with text.")#> [1] "Replace number with text."re_sub("\\s+","-","Split and join")#> [1] "Split-and-join"

Contributing

We welcome contributions to there package. If you have suggestions, bug reports, or want to contribute code, please open an issue or submit a pull request on our GitHub repository.

License

re is released under the MIT License. See the LICENSE file in the package’s repository for more details.

Links

License

Citation

Developers


[8]ページ先頭

©2009-2025 Movatter.jp