WenliL
WenliL

Reputation: 557

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

I want my string to only contain dash/alphabetic letter/number. I have converted all special characters to dash using

string %>% 
  gsub("[[:punct:]]", "-", .) %>%
  gsub(" ", "-", .) %>%
  gsub("\\-+", "-", .)

How to validate/remove last/first character(s) if they are not alphabetic/number in a string using regex?

Example input:

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

Expected output:

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

Upvotes: 2

Views: 268

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101247

Perhaps we can use

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

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

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626748

You can use

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

The gsub part replaces sequences of consecutive punctuation or whitespace chars with a single - char.

The trimws removes hyphens on both ends of each string item.

R test:

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

Another way to write this is

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

where [^[:alnum:]]+ matches one or more alphanumeric chars.

Upvotes: 2

Related Questions