aprilian
aprilian

Reputation: 650

Replace multiple consecutive hyphens in R

I have a string which looks like this:

something-------another--thing

I want to replace the multiple dashes with a single one.

So the expected output would be:

something-another-thing

Upvotes: 2

Views: 544

Answers (3)

Jeremy K.
Jeremy K.

Reputation: 1792

t2 <- "something-------another--thing"
library(stringr)
str_replace_all(t2, pattern = "-+", replacement = "-")

which gives:

[1] "something-another-thing"

If you're searching for the right regex to search for a string, you can test it out here https://regexr.com/

In the above, you're just searching for a pattern that is a hyphen, so pattern = "-", but we add the plus so that the search is 'greedy' and can include many hyphens, so we get pattern = "-+"

Upvotes: 0

G.Fernandes
G.Fernandes

Reputation: 321

you can do it with gsub and using regex.

> text='something-------another--thing'
> gsub('-{2,}','-',text)
[1] "something-another-thing"

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521457

We can try using sub here:

x <- "something-------another--thing"
gsub("-{2,}", "-", x)

[1] "something-another-thing"

More generally, if we want to replace any sequence of two or more of the same character with just the single character, then use this version:

x <- "something-------another--thing"
gsub("(.)\\1+", "\\1", x)

The second pattern could use an explanation:

(.)   match AND capture any single letter
\\1+  then match the same letter, at least one or possibly more times

Then, we replace with just the single captured letter.

Upvotes: 2

Related Questions