Remove Space inside Quotes
I'm trying to remove white spaces before and after a phrase which is placed inside double quotation marks. Whatever I've found on google removes the spaces alright but removes the spaces before and after the quotation marks too.
txt = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
# output:
"election laws"are outmoded or inadequate and often ambiguous"and should be changed."
This is the code:
import re
regex = r"(?<=["]) +| +(?=["])"
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
The expected output is:
"election laws "are outmoded or inadequate and often ambiguous" and should be changed."
Please help.
python regex
add a comment |
I'm trying to remove white spaces before and after a phrase which is placed inside double quotation marks. Whatever I've found on google removes the spaces alright but removes the spaces before and after the quotation marks too.
txt = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
# output:
"election laws"are outmoded or inadequate and often ambiguous"and should be changed."
This is the code:
import re
regex = r"(?<=["]) +| +(?=["])"
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
The expected output is:
"election laws "are outmoded or inadequate and often ambiguous" and should be changed."
Please help.
python regex
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05
add a comment |
I'm trying to remove white spaces before and after a phrase which is placed inside double quotation marks. Whatever I've found on google removes the spaces alright but removes the spaces before and after the quotation marks too.
txt = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
# output:
"election laws"are outmoded or inadequate and often ambiguous"and should be changed."
This is the code:
import re
regex = r"(?<=["]) +| +(?=["])"
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
The expected output is:
"election laws "are outmoded or inadequate and often ambiguous" and should be changed."
Please help.
python regex
I'm trying to remove white spaces before and after a phrase which is placed inside double quotation marks. Whatever I've found on google removes the spaces alright but removes the spaces before and after the quotation marks too.
txt = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
# output:
"election laws"are outmoded or inadequate and often ambiguous"and should be changed."
This is the code:
import re
regex = r"(?<=["]) +| +(?=["])"
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0)
if result:
print (result)
The expected output is:
"election laws "are outmoded or inadequate and often ambiguous" and should be changed."
Please help.
python regex
python regex
edited Nov 22 '18 at 18:45
xyres
9,44732345
9,44732345
asked Nov 22 '18 at 18:38
RonRon
257
257
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05
add a comment |
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05
add a comment |
4 Answers
4
active
oldest
votes
I don't think you can do this with regex (at least not at my level), you need to loop the string and count the occurrences of "
to remove space after if count is odd or before if it is even... (and this works only supposing they are always matched)
EDIT for cases where quotes are known to always be matched, see answer from Pedro Torres
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
add a comment |
The modified version of your code to work is:
import re
regex = '\"s+([^"]+)s+\"'
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed " second quotes "."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, '"'+r'1'+'"' , test_str)
if result:
print (result)
output:
election laws "are outmoded or inadequate and often ambiguous" and should be changed "second quotes".
Explanation:
I replace a match of " + spaces + (anything) + spaces + " with "+(anything)+"
where the () means capture group. So I can reference this capture group using the syntax r'1'
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
add a comment |
A possibility would be splitting the string and joining it afterward, applying different treatment to each chunk:
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
print(test_str)
test=test_str.split(""")
test[1]=test[1].strip()
test = """.join(test)
print(test)
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
add a comment |
I don't know python, but java. Briliant page about regexes is https://www.regular-expressions.info/ you can use that to adapt given regex or find another answer.
Your question depends, whether there is just one pair of quotation marks or not. If there is just one pair, the answer exists, say: regex:
^(.?") ?(.?) ?"(.*)$
replacement
$1$2"$3
if there are however multiple pairs, you have to worry about pairings start and end. Can they be nested or not? Can you guarantee, that what's inside of apostrophes cannot be single apostrophe? And even if you can do all that and guarantee, that it's always: 'start " end " start " end " ...', since each apostrophe has different handling depending whether it's start or end, you have to match whole segment and then repeat, which will lead to varying number of capturing groups. I believe that even the most ideal case is not possible via simple regex - replacement. And there are more issues with your problem, I believe, that will make it even more impossible.
Buch check that webpage, you won't find better documentation.
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%2f53436579%2fremove-space-inside-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I don't think you can do this with regex (at least not at my level), you need to loop the string and count the occurrences of "
to remove space after if count is odd or before if it is even... (and this works only supposing they are always matched)
EDIT for cases where quotes are known to always be matched, see answer from Pedro Torres
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
add a comment |
I don't think you can do this with regex (at least not at my level), you need to loop the string and count the occurrences of "
to remove space after if count is odd or before if it is even... (and this works only supposing they are always matched)
EDIT for cases where quotes are known to always be matched, see answer from Pedro Torres
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
add a comment |
I don't think you can do this with regex (at least not at my level), you need to loop the string and count the occurrences of "
to remove space after if count is odd or before if it is even... (and this works only supposing they are always matched)
EDIT for cases where quotes are known to always be matched, see answer from Pedro Torres
I don't think you can do this with regex (at least not at my level), you need to loop the string and count the occurrences of "
to remove space after if count is odd or before if it is even... (and this works only supposing they are always matched)
EDIT for cases where quotes are known to always be matched, see answer from Pedro Torres
edited Nov 28 '18 at 7:11
answered Nov 22 '18 at 18:45
SilmathoronSilmathoron
1,0211821
1,0211821
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
add a comment |
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
Thank You. This solves the problem.
– Ron
Nov 22 '18 at 22:47
add a comment |
The modified version of your code to work is:
import re
regex = '\"s+([^"]+)s+\"'
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed " second quotes "."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, '"'+r'1'+'"' , test_str)
if result:
print (result)
output:
election laws "are outmoded or inadequate and often ambiguous" and should be changed "second quotes".
Explanation:
I replace a match of " + spaces + (anything) + spaces + " with "+(anything)+"
where the () means capture group. So I can reference this capture group using the syntax r'1'
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
add a comment |
The modified version of your code to work is:
import re
regex = '\"s+([^"]+)s+\"'
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed " second quotes "."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, '"'+r'1'+'"' , test_str)
if result:
print (result)
output:
election laws "are outmoded or inadequate and often ambiguous" and should be changed "second quotes".
Explanation:
I replace a match of " + spaces + (anything) + spaces + " with "+(anything)+"
where the () means capture group. So I can reference this capture group using the syntax r'1'
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
add a comment |
The modified version of your code to work is:
import re
regex = '\"s+([^"]+)s+\"'
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed " second quotes "."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, '"'+r'1'+'"' , test_str)
if result:
print (result)
output:
election laws "are outmoded or inadequate and often ambiguous" and should be changed "second quotes".
Explanation:
I replace a match of " + spaces + (anything) + spaces + " with "+(anything)+"
where the () means capture group. So I can reference this capture group using the syntax r'1'
The modified version of your code to work is:
import re
regex = '\"s+([^"]+)s+\"'
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed " second quotes "."
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, '"'+r'1'+'"' , test_str)
if result:
print (result)
output:
election laws "are outmoded or inadequate and often ambiguous" and should be changed "second quotes".
Explanation:
I replace a match of " + spaces + (anything) + spaces + " with "+(anything)+"
where the () means capture group. So I can reference this capture group using the syntax r'1'
edited Nov 22 '18 at 21:36
answered Nov 22 '18 at 18:58
Pedro TorresPedro Torres
683413
683413
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
add a comment |
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
1
1
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
this does not work if there are several pairs of quotes
– Silmathoron
Nov 22 '18 at 21:19
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
Updated the answer so it does work with several pairs of quotes
– Pedro Torres
Nov 22 '18 at 21:37
add a comment |
A possibility would be splitting the string and joining it afterward, applying different treatment to each chunk:
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
print(test_str)
test=test_str.split(""")
test[1]=test[1].strip()
test = """.join(test)
print(test)
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
add a comment |
A possibility would be splitting the string and joining it afterward, applying different treatment to each chunk:
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
print(test_str)
test=test_str.split(""")
test[1]=test[1].strip()
test = """.join(test)
print(test)
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
add a comment |
A possibility would be splitting the string and joining it afterward, applying different treatment to each chunk:
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
print(test_str)
test=test_str.split(""")
test[1]=test[1].strip()
test = """.join(test)
print(test)
A possibility would be splitting the string and joining it afterward, applying different treatment to each chunk:
test_str = "election laws " are outmoded or inadequate and often ambiguous " and should be changed."
print(test_str)
test=test_str.split(""")
test[1]=test[1].strip()
test = """.join(test)
print(test)
answered Nov 22 '18 at 18:52
vctrdvctrd
10817
10817
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
add a comment |
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
Thank you. This helped.
– Ron
Nov 22 '18 at 22:48
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
No problem, note that the updated answer from Pedro Torres seems to works as well as should be possible, grouping together close quotes and ignoring unmatched quotes. Also, if one of these answers let you do what you wanted, please consider accepting it (again, I advise Pedro Torres)
– Silmathoron
Nov 23 '18 at 8:08
add a comment |
I don't know python, but java. Briliant page about regexes is https://www.regular-expressions.info/ you can use that to adapt given regex or find another answer.
Your question depends, whether there is just one pair of quotation marks or not. If there is just one pair, the answer exists, say: regex:
^(.?") ?(.?) ?"(.*)$
replacement
$1$2"$3
if there are however multiple pairs, you have to worry about pairings start and end. Can they be nested or not? Can you guarantee, that what's inside of apostrophes cannot be single apostrophe? And even if you can do all that and guarantee, that it's always: 'start " end " start " end " ...', since each apostrophe has different handling depending whether it's start or end, you have to match whole segment and then repeat, which will lead to varying number of capturing groups. I believe that even the most ideal case is not possible via simple regex - replacement. And there are more issues with your problem, I believe, that will make it even more impossible.
Buch check that webpage, you won't find better documentation.
add a comment |
I don't know python, but java. Briliant page about regexes is https://www.regular-expressions.info/ you can use that to adapt given regex or find another answer.
Your question depends, whether there is just one pair of quotation marks or not. If there is just one pair, the answer exists, say: regex:
^(.?") ?(.?) ?"(.*)$
replacement
$1$2"$3
if there are however multiple pairs, you have to worry about pairings start and end. Can they be nested or not? Can you guarantee, that what's inside of apostrophes cannot be single apostrophe? And even if you can do all that and guarantee, that it's always: 'start " end " start " end " ...', since each apostrophe has different handling depending whether it's start or end, you have to match whole segment and then repeat, which will lead to varying number of capturing groups. I believe that even the most ideal case is not possible via simple regex - replacement. And there are more issues with your problem, I believe, that will make it even more impossible.
Buch check that webpage, you won't find better documentation.
add a comment |
I don't know python, but java. Briliant page about regexes is https://www.regular-expressions.info/ you can use that to adapt given regex or find another answer.
Your question depends, whether there is just one pair of quotation marks or not. If there is just one pair, the answer exists, say: regex:
^(.?") ?(.?) ?"(.*)$
replacement
$1$2"$3
if there are however multiple pairs, you have to worry about pairings start and end. Can they be nested or not? Can you guarantee, that what's inside of apostrophes cannot be single apostrophe? And even if you can do all that and guarantee, that it's always: 'start " end " start " end " ...', since each apostrophe has different handling depending whether it's start or end, you have to match whole segment and then repeat, which will lead to varying number of capturing groups. I believe that even the most ideal case is not possible via simple regex - replacement. And there are more issues with your problem, I believe, that will make it even more impossible.
Buch check that webpage, you won't find better documentation.
I don't know python, but java. Briliant page about regexes is https://www.regular-expressions.info/ you can use that to adapt given regex or find another answer.
Your question depends, whether there is just one pair of quotation marks or not. If there is just one pair, the answer exists, say: regex:
^(.?") ?(.?) ?"(.*)$
replacement
$1$2"$3
if there are however multiple pairs, you have to worry about pairings start and end. Can they be nested or not? Can you guarantee, that what's inside of apostrophes cannot be single apostrophe? And even if you can do all that and guarantee, that it's always: 'start " end " start " end " ...', since each apostrophe has different handling depending whether it's start or end, you have to match whole segment and then repeat, which will lead to varying number of capturing groups. I believe that even the most ideal case is not possible via simple regex - replacement. And there are more issues with your problem, I believe, that will make it even more impossible.
Buch check that webpage, you won't find better documentation.
answered Nov 22 '18 at 19:23
Martin MuchaMartin Mucha
16710
16710
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%2f53436579%2fremove-space-inside-quotes%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
there is a modification of the code you posted that I put in one of the answers below that works for your input
– Pedro Torres
Nov 23 '18 at 0:05