I need to put a dot before a letter in this type of strings
name of data set: V2
6K102
62D102
627Z102
I would like to get this:
6.K102
62.D102
627.Z102
I am using this regex:
mutate(V2 = gsub("^[A-Z]",'\\.', V2))
I need to put a dot before a letter in this type of strings
name of data set: V2
6K102
62D102
627Z102
I would like to get this:
6.K102
62.D102
627.Z102
I am using this regex:
mutate(V2 = gsub("^[A-Z]",'\\.', V2))
If the string has to start with 1 or more digits followed by a char A-Z, you could use 2 capturing groups
^(\d+)([A-Z])
In the replacement use "\\1.\\2"
sub("^([0-9]+)([A-Z])", "\\1.\\2", V2)
This apply to the question before it was updated.
Usually \u2022 prints a bullet in text items. So if your question is regarding a label, you may just inserted it there "\u2022 ..."
Otherwise, for text items in datasets such as V2, you can work around by applying paste0 in combination with ifelse. In this case, your text items the you need a black dot infront if, is stored in V2$name
V2$name <- ifelse(V2$name==1,1,paste0("\u2022", V2$name))
Your regex lacks a capturing group around the letter pattern (so that you could keep it after replacement) and contains a redundant ^ anchor that matches the string start location. Also, you are using a gsub function while you just need a sub, since only one replacement is expected.
Use
sub("([[:upper:]])", ".\\1", V2)
With stringr (see demo):
stringr::str_replace(V2, "[[:upper:]]", "\\.\\0")
Details
sub - only the first match is replaced([[:upper:]]) - matches and captures any uppercase letter into Group 1 (later referenced to with \1 from the replacement pattern)\1 - the value of Group 1 (the uppercase letter matched)Note that stingr solution uses \0, the placeholder for the whole match value, so no need to capture the uppercase letter in the regex pattern.
See the R demo:
V2 <- c("6K102","62D102","627Z102")
sub("([[:upper:]])", ".\\1", V2)
# => [1] "6.K102" "62.D102" "627.Z102"