Replacer {regreplaceR} | R Documentation |
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")