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 |
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.
groups(pattern, s, ignore.case = FALSE)
groups(pattern, s, ignore.case = FALSE)
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. |
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.
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" groups(pattern, s) # $name # [1] "Jane" # $age # [1] "25"
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" groups(pattern, s) # $name # [1] "Jane" # $age # [1] "25"
This function extracts the value of a specific named group from a string using the provided regex pattern.
match_group(pattern, s, group_name, ignore.case = FALSE)
match_group(pattern, s, group_name, ignore.case = FALSE)
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. |
The extracted value of the specified named group, or NULL if the group is not found.
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" match_group(pattern, s, "name") # [1] "Jane"
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" match_group(pattern, s, "name") # [1] "Jane"
This function replaces the value of a specific named group in a string using the provided regex pattern.
replace_group(pattern, s, group_name, replacer, ignore.case = FALSE)
replace_group(pattern, s, group_name, replacer, ignore.case = FALSE)
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. |
A modified string where the matched value of the specified group has been replaced by the replacer
.
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" replace_group(pattern, s, "name", "John") # [1] "John is 25"
pattern <- "(?P<name>\\w+) is (?P<age>\\d+)" s <- "Jane is 25" replace_group(pattern, s, "name", "John") # [1] "John is 25"
The Replacer
class encapsulates regex operations using named groups.
It allows matching and replacing based on named groups in a regex pattern.
This class provides methods to match and replace parts of a string based on named groups in regular expressions.
re
A compiled regular expression pattern.
new()
Create a new Replacer object with a regex pattern.
Replacer$new( pattern = ".*?_(?P<date>\\d{8}-\\d{6}( \\(1\\))*)(?P<ext>\\..+$)" )
pattern
A character string with a regex pattern that includes named groups.
A new Replacer
object.
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6}( \\(1\\))*)(?P<ext>\\..+$)")
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
.
Replacer$match(s, group_name)
s
A character string to match against the regex pattern.
group_name
The name of the group to extract from the match.
The matched value of the group or NULL
if not found.
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)") r$match("file_20230905-123456.txt", "date")
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
.
Replacer$replace(s, group_name, replacement)
s
A character string to perform the replacement on.
group_name
The name of the group to be replaced.
replacement
The replacement string.
A modified string with the group replaced by the replacement.
r <- Replacer$new(pattern=".*?_(?P<date>\\d{8}-\\d{6})(?P<ext>\\..+$)") r$replace("file_20230905-123456.txt", "date", "20240905-123456")
clone()
The objects of this class are cloneable with this method.
Replacer$clone(deep = FALSE)
deep
Whether to make a deep clone.
# 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")
# 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")