Package 'regreplaceR'

Title: Match and Replace Strings Based on Named Groups in Regular Expressions
Description: An R6 class "Replacer" provided by the package simplifies working with regex patterns containing named groups. It allows easy retrieval of matched portions and targeted replacements by group name, improving both code clarity and maintainability.
Authors: Gwang-Jin Kim [aut, cre]
Maintainer: Gwang-Jin Kim <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2025-02-14 05:11:52 UTC
Source: https://github.com/gwangjinkim/regreplacer

Help Index


Extract Named Groups from a String Using a Regex Pattern

Description

This function extracts named groups from a given string using the specified regex pattern. Named groups in the pattern are denoted using ⁠(?P<group_name>...)⁠ syntax.

Usage

groups(pattern, s, ignore.case = FALSE)

Arguments

pattern

A character string containing the regex pattern with named groups.

s

A character string where the pattern will be searched.

ignore.case

Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE.

Value

A named list where names are the named groups and values are the extracted strings. If no matches are found, an empty list is returned.

Examples

pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
groups(pattern, s)
# $name
# [1] "Jane"
# $age
# [1] "25"

Extract a Specific Named Group from a String

Description

This function extracts the value of a specific named group from a string using the provided regex pattern.

Usage

match_group(pattern, s, group_name, ignore.case = FALSE)

Arguments

pattern

A character string containing the regex pattern with named groups.

s

A character string where the pattern will be searched.

group_name

The name of the group to extract from the match.

ignore.case

Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE.

Value

The extracted value of the specified named group, or NULL if the group is not found.

Examples

pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
match_group(pattern, s, "name")
# [1] "Jane"

Replace a Specific Named Group in a String

Description

This function replaces the value of a specific named group in a string using the provided regex pattern.

Usage

replace_group(pattern, s, group_name, replacer, ignore.case = FALSE)

Arguments

pattern

A character string containing the regex pattern with named groups.

s

A character string where the pattern will be searched.

group_name

The name of the group to be replaced.

replacer

A string that will replace the matched value of the specified group.

ignore.case

Logical. If TRUE, the pattern matching is case-insensitive. Default is FALSE.

Value

A modified string where the matched value of the specified group has been replaced by the replacer.

Examples

pattern <- "(?P<name>\\w+) is (?P<age>\\d+)"
s <- "Jane is 25"
replace_group(pattern, s, "name", "John")
# [1] "John is 25"

Replacer Class for Regex Operations

Description

The Replacer class encapsulates regex operations using named groups. It allows matching and replacing based on named groups in a regex pattern.

Details

This class provides methods to match and replace parts of a string based on named groups in regular expressions.

Public fields

re

A compiled regular expression pattern.

Methods

Public methods


Method new()

Create a new Replacer object with a regex pattern.

Usage
Replacer$new(
  pattern = ".*?_(?P<date>\\d{8}-\\d{6}( \\(1\\))*)(?P<ext>\\..+$)"
)
Arguments
pattern

A character string with a regex pattern that includes named groups.

Returns

A new Replacer object.

Examples
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6}( \\(1\\))*)(?P<ext>\\..+$)")

Method match()

Match a string and extract a group by its name.

This method matches the string using the regex pattern and extracts the value of the group specified by group_name.

Usage
Replacer$match(s, group_name)
Arguments
s

A character string to match against the regex pattern.

group_name

The name of the group to extract from the match.

Returns

The matched value of the group or NULL if not found.

Examples
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)")
r$match("file_20230905-123456.txt", "date")

Method replace()

Replace the value of a matched group with a replacement string.

This method replaces the value of the matched group specified by group_name with the replacement string in the input string s.

Usage
Replacer$replace(s, group_name, replacement)
Arguments
s

A character string to perform the replacement on.

group_name

The name of the group to be replaced.

replacement

The replacement string.

Returns

A modified string with the group replaced by the replacement.

Examples
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)")
r$replace("file_20230905-123456.txt", "date", "20240905-123456")

Method clone()

The objects of this class are cloneable with this method.

Usage
Replacer$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

# Create a new Replacer object
r <- Replacer$new(pattern = ".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)")

# Match a group within a string
r$match("file_20230905-123456.txt", "date")

# Replace the value of a matched group
r$replace("file_20230905-123456.txt", "date", "20240905-123456")

## ------------------------------------------------
## Method `Replacer$new`
## ------------------------------------------------

r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6}( \\(1\\))*)(?P<ext>\\..+$)")

## ------------------------------------------------
## Method `Replacer$match`
## ------------------------------------------------

r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)")
r$match("file_20230905-123456.txt", "date")

## ------------------------------------------------
## Method `Replacer$replace`
## ------------------------------------------------

r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)")
r$replace("file_20230905-123456.txt", "date", "20240905-123456")