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"
CodePudding user response:
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.
CodePudding user response:
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"