Remove Space inside Quotes












4















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.










share|improve this question

























  • 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
















4















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.










share|improve this question

























  • 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














4












4








4


0






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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












4 Answers
4






active

oldest

votes


















3














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






share|improve this answer


























  • Thank You. This solves the problem.

    – Ron
    Nov 22 '18 at 22:47





















2














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'






share|improve this answer





















  • 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














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)





share|improve this answer
























  • 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



















1














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    3














    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






    share|improve this answer


























    • Thank You. This solves the problem.

      – Ron
      Nov 22 '18 at 22:47


















    3














    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






    share|improve this answer


























    • Thank You. This solves the problem.

      – Ron
      Nov 22 '18 at 22:47
















    3












    3








    3







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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





















    • 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















    2














    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'






    share|improve this answer





















    • 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
















    2














    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'






    share|improve this answer





















    • 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














    2












    2








    2







    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'






    share|improve this answer















    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'







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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














    • 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











    1














    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)





    share|improve this answer
























    • 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
















    1














    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)





    share|improve this answer
























    • 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














    1












    1








    1







    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)





    share|improve this answer













    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)






    share|improve this answer












    share|improve this answer



    share|improve this answer










    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



















    • 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











    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 19:23









        Martin MuchaMartin Mucha

        16710




        16710






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'