Add number in the string after each letter
I have several strings with a fixed format.
The format is one letter followed by a number, e.g., A3B1C7D1.
However, if the number behind a letter is 1, the string is written as A3BC7D.
What I want to do is to insert number 1, and convert the string from A3BC7D
to A3B1C7D1
.
My example data is
strings <- c("A", "A3BC3", "A2B1C")
What I want to get is:
strings_new <- c("A1", "A3B1C3", "A2B1C1")
Thanks a lot!
r regex string
add a comment |
I have several strings with a fixed format.
The format is one letter followed by a number, e.g., A3B1C7D1.
However, if the number behind a letter is 1, the string is written as A3BC7D.
What I want to do is to insert number 1, and convert the string from A3BC7D
to A3B1C7D1
.
My example data is
strings <- c("A", "A3BC3", "A2B1C")
What I want to get is:
strings_new <- c("A1", "A3B1C3", "A2B1C1")
Thanks a lot!
r regex string
add a comment |
I have several strings with a fixed format.
The format is one letter followed by a number, e.g., A3B1C7D1.
However, if the number behind a letter is 1, the string is written as A3BC7D.
What I want to do is to insert number 1, and convert the string from A3BC7D
to A3B1C7D1
.
My example data is
strings <- c("A", "A3BC3", "A2B1C")
What I want to get is:
strings_new <- c("A1", "A3B1C3", "A2B1C1")
Thanks a lot!
r regex string
I have several strings with a fixed format.
The format is one letter followed by a number, e.g., A3B1C7D1.
However, if the number behind a letter is 1, the string is written as A3BC7D.
What I want to do is to insert number 1, and convert the string from A3BC7D
to A3B1C7D1
.
My example data is
strings <- c("A", "A3BC3", "A2B1C")
What I want to get is:
strings_new <- c("A1", "A3B1C3", "A2B1C1")
Thanks a lot!
r regex string
r regex string
edited Feb 22 at 11:28
markus
14.8k11336
14.8k11336
asked Feb 22 at 11:08
WangWang
20119
20119
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Another option:
gsub("([A-Za-z])(?=[A-Za-z])|([A-Za-z])$", "\1\21", strings, perl = T)
Output:
[1] "A1" "A3B1C3" "A2B1C1"
Or if you only have capitals, just:
gsub("([A-Z])(?=[A-Z])|([A-Z])$", "\1\21", strings, perl = T)
Basically this finds letters that are either followed by another letter or are at the end of string, and replaces them with themselves while at the same time adds the desired number, 1
in this case.
add a comment |
Find all (uppercase) letters ([A-Z]
) that is not followed by a number and replace it with that string + 1
:
gsub("([A-Z])(?![0-9])", "\11", strings, perl = TRUE)
# [1] "A1" "A3B1C3" "A2B1C1"
add a comment |
strings[!grepl("[0-9]$",strings)]=paste0(strings[!grepl("[0-9]$",strings)],"1")
[1] "A1" "A3BC3" "A2B1C1"
We first grep
all positions that do not end with a number, and paste a 1
to them.
9
Almost. Check the second element it should be"A3B1C3"
– markus
Feb 22 at 11:19
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54825764%2fadd-number-in-the-string-after-each-letter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Another option:
gsub("([A-Za-z])(?=[A-Za-z])|([A-Za-z])$", "\1\21", strings, perl = T)
Output:
[1] "A1" "A3B1C3" "A2B1C1"
Or if you only have capitals, just:
gsub("([A-Z])(?=[A-Z])|([A-Z])$", "\1\21", strings, perl = T)
Basically this finds letters that are either followed by another letter or are at the end of string, and replaces them with themselves while at the same time adds the desired number, 1
in this case.
add a comment |
Another option:
gsub("([A-Za-z])(?=[A-Za-z])|([A-Za-z])$", "\1\21", strings, perl = T)
Output:
[1] "A1" "A3B1C3" "A2B1C1"
Or if you only have capitals, just:
gsub("([A-Z])(?=[A-Z])|([A-Z])$", "\1\21", strings, perl = T)
Basically this finds letters that are either followed by another letter or are at the end of string, and replaces them with themselves while at the same time adds the desired number, 1
in this case.
add a comment |
Another option:
gsub("([A-Za-z])(?=[A-Za-z])|([A-Za-z])$", "\1\21", strings, perl = T)
Output:
[1] "A1" "A3B1C3" "A2B1C1"
Or if you only have capitals, just:
gsub("([A-Z])(?=[A-Z])|([A-Z])$", "\1\21", strings, perl = T)
Basically this finds letters that are either followed by another letter or are at the end of string, and replaces them with themselves while at the same time adds the desired number, 1
in this case.
Another option:
gsub("([A-Za-z])(?=[A-Za-z])|([A-Za-z])$", "\1\21", strings, perl = T)
Output:
[1] "A1" "A3B1C3" "A2B1C1"
Or if you only have capitals, just:
gsub("([A-Z])(?=[A-Z])|([A-Z])$", "\1\21", strings, perl = T)
Basically this finds letters that are either followed by another letter or are at the end of string, and replaces them with themselves while at the same time adds the desired number, 1
in this case.
edited Feb 22 at 11:46
answered Feb 22 at 11:38
arg0nautarg0naut
6,0141421
6,0141421
add a comment |
add a comment |
Find all (uppercase) letters ([A-Z]
) that is not followed by a number and replace it with that string + 1
:
gsub("([A-Z])(?![0-9])", "\11", strings, perl = TRUE)
# [1] "A1" "A3B1C3" "A2B1C1"
add a comment |
Find all (uppercase) letters ([A-Z]
) that is not followed by a number and replace it with that string + 1
:
gsub("([A-Z])(?![0-9])", "\11", strings, perl = TRUE)
# [1] "A1" "A3B1C3" "A2B1C1"
add a comment |
Find all (uppercase) letters ([A-Z]
) that is not followed by a number and replace it with that string + 1
:
gsub("([A-Z])(?![0-9])", "\11", strings, perl = TRUE)
# [1] "A1" "A3B1C3" "A2B1C1"
Find all (uppercase) letters ([A-Z]
) that is not followed by a number and replace it with that string + 1
:
gsub("([A-Z])(?![0-9])", "\11", strings, perl = TRUE)
# [1] "A1" "A3B1C3" "A2B1C1"
edited Feb 22 at 13:44
answered Feb 22 at 13:18
sindri_baldursindri_baldur
8,3151033
8,3151033
add a comment |
add a comment |
strings[!grepl("[0-9]$",strings)]=paste0(strings[!grepl("[0-9]$",strings)],"1")
[1] "A1" "A3BC3" "A2B1C1"
We first grep
all positions that do not end with a number, and paste a 1
to them.
9
Almost. Check the second element it should be"A3B1C3"
– markus
Feb 22 at 11:19
add a comment |
strings[!grepl("[0-9]$",strings)]=paste0(strings[!grepl("[0-9]$",strings)],"1")
[1] "A1" "A3BC3" "A2B1C1"
We first grep
all positions that do not end with a number, and paste a 1
to them.
9
Almost. Check the second element it should be"A3B1C3"
– markus
Feb 22 at 11:19
add a comment |
strings[!grepl("[0-9]$",strings)]=paste0(strings[!grepl("[0-9]$",strings)],"1")
[1] "A1" "A3BC3" "A2B1C1"
We first grep
all positions that do not end with a number, and paste a 1
to them.
strings[!grepl("[0-9]$",strings)]=paste0(strings[!grepl("[0-9]$",strings)],"1")
[1] "A1" "A3BC3" "A2B1C1"
We first grep
all positions that do not end with a number, and paste a 1
to them.
answered Feb 22 at 11:18
boskiboski
661415
661415
9
Almost. Check the second element it should be"A3B1C3"
– markus
Feb 22 at 11:19
add a comment |
9
Almost. Check the second element it should be"A3B1C3"
– markus
Feb 22 at 11:19
9
9
Almost. Check the second element it should be
"A3B1C3"
– markus
Feb 22 at 11:19
Almost. Check the second element it should be
"A3B1C3"
– markus
Feb 22 at 11:19
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54825764%2fadd-number-in-the-string-after-each-letter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown