regreplacer

Match and replace named groups in Regex

https://github.com/gwangjinkim/regreplacer

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.7%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Match and replace named groups in Regex

Basic Info
  • Host: GitHub
  • Owner: gwangjinkim
  • License: other
  • Language: R
  • Default Branch: master
  • Size: 392 KB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created almost 2 years ago · Last pushed almost 2 years ago
Metadata Files
Readme Changelog License

README.md

regreplaceR

This R package provides 4 tiny regex tools to extract, match, and replace named regex groups in strings. It contains in addition to 3 functions groups(), match_group() and replace_group() a R6 class Replacer which is thought as the main interface of this package.

Installation

You can install this package by installing it from CRAN or from this GitHub repository.

```r

install directly from CRAN

install.packages("regreplaceR")

or install directly from this github repository

install the devtools package if not already installed

install.packages("devtools")

install the package directly from this repository

devtools::install_github("gwangjinkim/regreplaceR") ```

Overview

The package offers three core functions to interact with strings using regular expressions with named groups:

  • groups(): Extract all named groups from a string.
  • match_group(): Extract a specific named group.
  • replace_group(): Replace the value of a specific named group.

Additionally, it includes an R6 class called Replacer that encapsulates these functionalities for a more object-oriented approach to regex-based operations.

I couldn't use match() and replace() as generic functions, because there are already generic functions which require a specific set of arguments. By using an R6 class, I was free to choose the arguments for match() and replace().

1. groups()

This function extracts all named groups from a string based on a provided regex pattern.

```r pattern <- "(?P\w+) is (?P\d+)" s <- "Jane is 25" result <- groups(pattern, s)

Output:

$name

[1] "Jane"

$age

[1] "25"

```

2. match_group()

Extract the value of a specific named group from the string.

```r pattern <- "(?P\w+) is (?P\d+)" s <- "Jane is 25" name <- match_group(pattern, s, "name")

Output:

[1] "Jane"

```

3. replace_group()

Replace the value of a specific named group with a new string.

```r pattern <- "(?P\w+) is (?P\d+)" s <- "Jane is 25" modified <- replace_group(pattern, s, "name", "John")

Output:

[1] "John is 25"

```

4. The Replacer Class

The Replacer class provides an object-oriented approach to regex-based operations. It allows you to create an instance with a specific regex pattern and then use it to match or replace named groups in strings.

Example Usage

```r

Create a new Replacer object

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

Match a group within a string

date <- r$match("file_20230905-123456.txt", "date")

Replace the value of a matched group

modifiedstring <- r$replace("file20230905-123456.txt", "date", "20240905-123456") ```

Class Methods

  • match(): Extracts the value of a named group from the string.
  • replace(): Replaces the value of a named group with a specified string.

License

This package is licensed under the MIT License. See LICENSE for details.

Happy regexing! If you have any questions or find any bugs, please feel free to open an issue on the GitHub repository.

Owner

  • Name: Gwang-Jin Kim
  • Login: gwangjinkim
  • Kind: user
  • Location: Freiburg i. Br., Germany
  • Company: Pharma

Data Scientist, Bioinformatician, and Human Geneticist.

GitHub Events

Total
Last Year

Packages

  • Total packages: 1
  • Total downloads:
    • cran 172 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 1
  • Total maintainers: 1
cran.r-project.org: regreplaceR

Match and Replace Strings Based on Named Groups in Regular Expressions

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 172 Last month
Rankings
Dependent packages count: 28.2%
Dependent repos count: 34.8%
Average: 49.9%
Downloads: 86.7%
Maintainers (1)
Last synced: 11 months ago

Dependencies

..Rcheck/regreplace/DESCRIPTION cran
  • R6 * imports
  • knitr * suggests
  • rmarkdown * suggests
DESCRIPTION cran
  • R6 * imports
  • knitr * suggests
  • rmarkdown * suggests
regreplaceR.Rcheck/00_pkg_src/regreplaceR/DESCRIPTION cran
  • R6 * imports
  • knitr * suggests
  • rmarkdown * suggests
regreplaceR.Rcheck/regreplaceR/DESCRIPTION cran
  • R6 * imports
  • knitr * suggests
  • rmarkdown * suggests