Home > Net >  Match only the second and subsequent hyphens in a regex
Match only the second and subsequent hyphens in a regex

Time:12-10

My goal is to match ONLY the hyphens in a string after the first one so I can remove them with null spaces in R.

Example:

This test - and it is a test - should work - very well.

Desired outcome:

This test - and it is a test should work very well.

I've tried (?<=\-).*?\- but that matches the contents between the hyphens as well. The string may have a variable number of hyphens, but I only want to preserve the first one.

What should I use?

CodePudding user response:

We could do it this way:

string <- "This test - and it is a test - should work - very well."

gsub("^([^-]*\\-)|\\-", "\\1", string, perl=TRUE)

[1] "This test - and it is a test  should work  very well."

CodePudding user response:

We could use SKIP/FAIL to skip the first -

gsub("^[^-] -(*SKIP)(*FAIL)|\\s*-", "", str1, perl = TRUE)

-output

[1] "This test - and it is a test should work very well."

data

str1 <- "This test - and it is a test - should work - very well."
  • Related