如果 last/first 个字符不是 alphabetic/number,如何删除它们?

How to remove last/first character(s) if they are not alphabetic/number?

我希望我的字符串只包含 dash/alphabetic letter/number。我已使用

将所有特殊字符转换为破折号
string %>% 
  gsub("[[:punct:]]", "-", .) %>%
  gsub(" ", "-", .) %>%
  gsub("\-+", "-", .)

如何使用正则表达式 validate/remove last/first 个字符,如果它们不是 alphabetic/number 个字符串?

示例输入:

"-example-one111-"  "-222example-two" "333example-333three333-"

预期输出:

"example-one111"  "222example-two" "333example-333three333"

您可以使用

trimws(gsub("[[:punct:][:space:]]+", "-", string), whitespace="-")

gsub 部分用单个 - 字符替换连续的标点符号序列或空白字符。

trimws 删除每个字符串项两端的连字符。

R 测试:

> string <- c("-example-one111-", "---222example-two", "333example-333three333----")
> trimws(gsub("[[:punct:][:space:]]+", "-", string), whitespace="-")
[1] "example-one111"         "222example-two"         "333example-333three333"

另一种写法是

trimws(gsub("[^[:alnum:]]+", "-", string), whitespace="-")

其中 [^[:alnum:]]+ 匹配一个或多个字母数字字符。

也许我们可以使用

> gsub(".*?([[:alnum:]].*[[:alnum:]]).*", "\1", string)
[1] "example-one111"         "222example-two"         "333example-333three333"

> gsub("^[^[:alnum:]]+|[^[:alnum:]]+$", "", string)
[1] "example-one111"         "222example-two"         "333example-333three333"