ID EN
String Functions

regmatches

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Ekstrak atau ganti substring yang cocok dari data kecocokan yang diperoleh regexpr, gregexpr, atau regexec.

Syntax

R
regmatches(x, m, invert = FALSE)
regmatches(x, m, invert = FALSE) <- value

Arguments

Parameter Deskripsi
x a character vector
m an object with match data
invert a logical: if TRUE, extract or replace the non-matched substrings.
value an object with suitable replacement values for the matched or non-matched substrings (see Details).

Return Value

Untuk pencocokan ulang, vektor karakter dengan substring yang cocok jika m adalah vektor dan invert adalah FALSE. Jika tidak, daftar dengan substring yang cocok atau/dan tidak cocok. Untuk regmatches<-, vektor karakter yang diperbarui.

Details

Jika invert adalah FALSE (default), regmatches mengekstrak substring yang cocok seperti yang ditentukan oleh data kecocokan. Untuk data pencocokan vektor (yang diperoleh dari regexpr), pencocokan kosong akan dihilangkan; untuk data pencocokan daftar, pencocokan kosong memberikan komponen kosong (vektor karakter dengan panjang nol). Jika invert adalah TRUE, regmatches mengekstrak substring yang tidak cocok, yaitu, string dipecah berdasarkan kecocokan yang mirip dengan strsplit (untuk data pencocokan vektor, paling banyak satu pemisahan dilakukan). Jika invert adalah NA, regmatches mengekstrak substring yang tidak cocok dan yang cocok, selalu dimulai dan diakhiri dengan yang tidak cocok (kosong jika kecocokan terjadi di awal atau di akhir). Perhatikan bahwa data kecocokan dapat diperoleh dari pencocokan ekspresi reguler pada versi x yang dimodifikasi dengan jumlah chara yang sama

Contoh

Example
R
# NOT RUN {
x <- c("A and B", "A, B and C", "A, B, C and D", "foobar")
pattern <- "[[:space:]]*(,|and)[[:space:]]"
## Match data from regexpr()
m <- regexpr(pattern, x)
regmatches(x, m)
regmatches(x, m, invert = TRUE)
## Match data from gregexpr()
m <- gregexpr(pattern, x)
regmatches(x, m)
regmatches(x, m, invert = TRUE)

## Consider
x <- "John (fishing, hunting), Paul (hiking, biking)"
## Suppose we want to split at the comma (plus spaces) between the
## persons, but not at the commas in the parenthesized hobby lists.
## One idea is to "blank out" the parenthesized parts to match the
## parts to be used for splitting, and extract the persons as the
## non-matched parts.
## First, match the parenthesized hobby lists.
m <- gregexpr("\\([^)]*\\)", x)
## Create blank strings with given numbers of characters.
blanks <- function(n) strrep(" ", n)
## Create a copy of x with the parenthesized parts blanked out.
s <- x
regmatches(s, m) <- Map(blanks, lapply(regmatches(s, m), nchar))
s
## Compute the positions of the split matches (note that we cannot call
## strsplit() on x with match data from s).
m <- gregexpr(", *", s)
## And finally extract the non-matched parts.
regmatches(x, m, invert = TRUE)
# }