Remove multiple punctuations scattered throughout string
These are the default column names produced.
columnNames
[1] "chain:1.theta[1]" "chain:1.theta[2]" "chain:1.theta[3]" "chain:1.theta[4]"
I would like columnNames to be:
[1] "theta1" "theta2" "theta3" "theta4"
I would like to do this using one regular expression. I have tried a few different approaches with no success.
> gsub('chain:[[:digit:]][[:punct:]]', '', columnNames)
[1] "theta[1]" "theta[2]" "theta[3]" "theta[4]"
> gsub('chain:[[:digit:]].\[|\]', '', columnNames)
[1] "chain:1.theta[1" "chain:4.theta[2" "chain:1.theta[3" "chain:4.theta[4"
> gsub('(?=.*chain:[[:digit:]][[:punct:]])(?=.*"\[|\])', '', columnNames, perl = TRUE)
[1] "chain:1.theta[1]" "chain:4.theta[2]" "chain:1.theta[3]" "chain:4.theta[4]
> gsub('(?!theta\[[:digit:]])', '', columnNames, perl = TRUE)
Error in gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
invalid regular expression '(?!theta[[:digit:]])'
In addition: Warning message:
In gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
PCRE pattern compilation error
'POSIX named classes are supported only within a class'
at '[:digit:]])'
r regex regex-lookarounds
add a comment |
These are the default column names produced.
columnNames
[1] "chain:1.theta[1]" "chain:1.theta[2]" "chain:1.theta[3]" "chain:1.theta[4]"
I would like columnNames to be:
[1] "theta1" "theta2" "theta3" "theta4"
I would like to do this using one regular expression. I have tried a few different approaches with no success.
> gsub('chain:[[:digit:]][[:punct:]]', '', columnNames)
[1] "theta[1]" "theta[2]" "theta[3]" "theta[4]"
> gsub('chain:[[:digit:]].\[|\]', '', columnNames)
[1] "chain:1.theta[1" "chain:4.theta[2" "chain:1.theta[3" "chain:4.theta[4"
> gsub('(?=.*chain:[[:digit:]][[:punct:]])(?=.*"\[|\])', '', columnNames, perl = TRUE)
[1] "chain:1.theta[1]" "chain:4.theta[2]" "chain:1.theta[3]" "chain:4.theta[4]
> gsub('(?!theta\[[:digit:]])', '', columnNames, perl = TRUE)
Error in gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
invalid regular expression '(?!theta[[:digit:]])'
In addition: Warning message:
In gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
PCRE pattern compilation error
'POSIX named classes are supported only within a class'
at '[:digit:]])'
r regex regex-lookarounds
add a comment |
These are the default column names produced.
columnNames
[1] "chain:1.theta[1]" "chain:1.theta[2]" "chain:1.theta[3]" "chain:1.theta[4]"
I would like columnNames to be:
[1] "theta1" "theta2" "theta3" "theta4"
I would like to do this using one regular expression. I have tried a few different approaches with no success.
> gsub('chain:[[:digit:]][[:punct:]]', '', columnNames)
[1] "theta[1]" "theta[2]" "theta[3]" "theta[4]"
> gsub('chain:[[:digit:]].\[|\]', '', columnNames)
[1] "chain:1.theta[1" "chain:4.theta[2" "chain:1.theta[3" "chain:4.theta[4"
> gsub('(?=.*chain:[[:digit:]][[:punct:]])(?=.*"\[|\])', '', columnNames, perl = TRUE)
[1] "chain:1.theta[1]" "chain:4.theta[2]" "chain:1.theta[3]" "chain:4.theta[4]
> gsub('(?!theta\[[:digit:]])', '', columnNames, perl = TRUE)
Error in gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
invalid regular expression '(?!theta[[:digit:]])'
In addition: Warning message:
In gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
PCRE pattern compilation error
'POSIX named classes are supported only within a class'
at '[:digit:]])'
r regex regex-lookarounds
These are the default column names produced.
columnNames
[1] "chain:1.theta[1]" "chain:1.theta[2]" "chain:1.theta[3]" "chain:1.theta[4]"
I would like columnNames to be:
[1] "theta1" "theta2" "theta3" "theta4"
I would like to do this using one regular expression. I have tried a few different approaches with no success.
> gsub('chain:[[:digit:]][[:punct:]]', '', columnNames)
[1] "theta[1]" "theta[2]" "theta[3]" "theta[4]"
> gsub('chain:[[:digit:]].\[|\]', '', columnNames)
[1] "chain:1.theta[1" "chain:4.theta[2" "chain:1.theta[3" "chain:4.theta[4"
> gsub('(?=.*chain:[[:digit:]][[:punct:]])(?=.*"\[|\])', '', columnNames, perl = TRUE)
[1] "chain:1.theta[1]" "chain:4.theta[2]" "chain:1.theta[3]" "chain:4.theta[4]
> gsub('(?!theta\[[:digit:]])', '', columnNames, perl = TRUE)
Error in gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
invalid regular expression '(?!theta[[:digit:]])'
In addition: Warning message:
In gsub("(?!theta\[[:digit:]])", "", columnNames, perl = TRUE) :
PCRE pattern compilation error
'POSIX named classes are supported only within a class'
at '[:digit:]])'
r regex regex-lookarounds
r regex regex-lookarounds
asked Nov 27 '18 at 1:33
qq3254qq3254
508
508
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
gsub(".*\.(.*)\[(\d+)\]", "\1\2", columnNames)
[1] "theta1" "theta2" "theta3" "theta4"
where .*\. matches everything up to and including a dot, (.*) corresponds to theta in this case, and (\d+) to the theta numbers.
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%2f53491515%2fremove-multiple-punctuations-scattered-throughout-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
gsub(".*\.(.*)\[(\d+)\]", "\1\2", columnNames)
[1] "theta1" "theta2" "theta3" "theta4"
where .*\. matches everything up to and including a dot, (.*) corresponds to theta in this case, and (\d+) to the theta numbers.
add a comment |
gsub(".*\.(.*)\[(\d+)\]", "\1\2", columnNames)
[1] "theta1" "theta2" "theta3" "theta4"
where .*\. matches everything up to and including a dot, (.*) corresponds to theta in this case, and (\d+) to the theta numbers.
add a comment |
gsub(".*\.(.*)\[(\d+)\]", "\1\2", columnNames)
[1] "theta1" "theta2" "theta3" "theta4"
where .*\. matches everything up to and including a dot, (.*) corresponds to theta in this case, and (\d+) to the theta numbers.
gsub(".*\.(.*)\[(\d+)\]", "\1\2", columnNames)
[1] "theta1" "theta2" "theta3" "theta4"
where .*\. matches everything up to and including a dot, (.*) corresponds to theta in this case, and (\d+) to the theta numbers.
answered Nov 27 '18 at 1:38
Julius VainoraJulius Vainora
38k76584
38k76584
add a comment |
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%2f53491515%2fremove-multiple-punctuations-scattered-throughout-string%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