regex find substring and replace characters and update whole string












3














From



string= this is, not good "type of ,question" to ask, on stackoverflow


I want to extract "type of , question" substring and replace ',' with ' '.



with re.findall() it yields a list of characters between " " and with re.search it yields class object.



With re.sub() it replaces all ',' but I need them except the ones that are inside sub-string with double quotes.



Can anyone help me with this problem.



Thanks in advance!!










share|improve this question
























  • It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
    – Kevin
    Nov 20 at 16:27










  • output: this is, not good "type of question" to ask, on stackoverflow
    – Devanshu Khokhani
    Nov 20 at 16:27










  • Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
    – Daniel F
    Nov 20 at 16:29










  • What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
    – Kevin
    Nov 20 at 16:29






  • 1




    If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
    – Kevin
    Nov 20 at 16:33
















3














From



string= this is, not good "type of ,question" to ask, on stackoverflow


I want to extract "type of , question" substring and replace ',' with ' '.



with re.findall() it yields a list of characters between " " and with re.search it yields class object.



With re.sub() it replaces all ',' but I need them except the ones that are inside sub-string with double quotes.



Can anyone help me with this problem.



Thanks in advance!!










share|improve this question
























  • It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
    – Kevin
    Nov 20 at 16:27










  • output: this is, not good "type of question" to ask, on stackoverflow
    – Devanshu Khokhani
    Nov 20 at 16:27










  • Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
    – Daniel F
    Nov 20 at 16:29










  • What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
    – Kevin
    Nov 20 at 16:29






  • 1




    If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
    – Kevin
    Nov 20 at 16:33














3












3








3







From



string= this is, not good "type of ,question" to ask, on stackoverflow


I want to extract "type of , question" substring and replace ',' with ' '.



with re.findall() it yields a list of characters between " " and with re.search it yields class object.



With re.sub() it replaces all ',' but I need them except the ones that are inside sub-string with double quotes.



Can anyone help me with this problem.



Thanks in advance!!










share|improve this question















From



string= this is, not good "type of ,question" to ask, on stackoverflow


I want to extract "type of , question" substring and replace ',' with ' '.



with re.findall() it yields a list of characters between " " and with re.search it yields class object.



With re.sub() it replaces all ',' but I need them except the ones that are inside sub-string with double quotes.



Can anyone help me with this problem.



Thanks in advance!!







python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 16:23









anubhava

519k46316389




519k46316389










asked Nov 20 at 16:20









Devanshu Khokhani

428




428












  • It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
    – Kevin
    Nov 20 at 16:27










  • output: this is, not good "type of question" to ask, on stackoverflow
    – Devanshu Khokhani
    Nov 20 at 16:27










  • Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
    – Daniel F
    Nov 20 at 16:29










  • What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
    – Kevin
    Nov 20 at 16:29






  • 1




    If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
    – Kevin
    Nov 20 at 16:33


















  • It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
    – Kevin
    Nov 20 at 16:27










  • output: this is, not good "type of question" to ask, on stackoverflow
    – Devanshu Khokhani
    Nov 20 at 16:27










  • Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
    – Daniel F
    Nov 20 at 16:29










  • What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
    – Kevin
    Nov 20 at 16:29






  • 1




    If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
    – Kevin
    Nov 20 at 16:33
















It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
– Kevin
Nov 20 at 16:27




It sounds like you already tried using re.findall, re.search, and re.sub, yes? Please share the code for each of those attempts.
– Kevin
Nov 20 at 16:27












output: this is, not good "type of question" to ask, on stackoverflow
– Devanshu Khokhani
Nov 20 at 16:27




output: this is, not good "type of question" to ask, on stackoverflow
– Devanshu Khokhani
Nov 20 at 16:27












Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
– Daniel F
Nov 20 at 16:29




Is it 'string= this is, not good "type of ,question" to ask, on stackoverflow' or string = 'this is, not good "type of ,question" to ask, on stackoverflow'?
– Daniel F
Nov 20 at 16:29












What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
– Kevin
Nov 20 at 16:29




What should happen if there are more than two quote marks in the string? What if there are an odd number of quote marks? What if there are two 'real' quote marks, and one escaped quote mark inside that quote?
– Kevin
Nov 20 at 16:29




1




1




If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
– Kevin
Nov 20 at 16:33




If the only data the code needs to work on is the one example you gave, then you only have to do result = 'this is, not good "type of question" to ask, on stackoverflow'. If you're thinking "very funny, I actually need it to work on a variety of inputs", then that's exactly why I'm asking these clarifying questions :-)
– Kevin
Nov 20 at 16:33












5 Answers
5






active

oldest

votes


















4














Use regex capture groups:



import re
s= 'this is, not good "type of ,question" to ask, on stackoverflow'
re.sub(r'(".*?),(.*?")', r'12', s)


output:



'this is, not good "type of question" to ask, on stackoverflow'


Explanation: (stuff) in regex stands for capture groups, 1 and 2 respectively substitutes the part before and after the , character within the quoted part of string. Please note this also works for multiple quotes within a single string as well.






share|improve this answer























  • this one worked perfectly for me. Thanks for helping me out @Rocky Li
    – Devanshu Khokhani
    Nov 20 at 16:34










  • Glad to be able to help
    – Rocky Li
    Nov 20 at 16:34










  • Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
    – Daniel F
    Nov 20 at 16:53






  • 1




    This would only remove one instance of comma though, which is fair if that's all OP need.
    – Idlehands
    Nov 20 at 16:57












  • Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
    – Daniel F
    Nov 20 at 16:59





















2














Another way that gives you some flexibility is you can do it by two steps:




  1. Find all the matches that are contained in quotations,


  2. In each match look for and replace the ','.



Example:



# define a pattern that gets you everything inside a double quote
pat = re.compile(r'"[^"]+"')

# re.sub the quote pattern and replace the , in each of those matches.
string = pat.sub(lambda x: x.group(0).replace(',',''), string)

# 'this is, not good "type of question" to ask, on stackoverflow'


The flexibility of this is it allows you to replace as many ',' as you need, and you can perform other changes as well once you have located all the double quote patterns.






share|improve this answer





















  • This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
    – Rocky Li
    Nov 20 at 17:14



















1














How about a combination of split() and replace()? :



s = 'this is, not good "type of ,question" to ask, on stackoverflow'

splitted = s.split('"')
print(s.replace(splitted[1], splitted[1].replace(',', '')))

# this is, not good "type of question" to ask, on stackoverflow


Note: This works in this case, but does not work in cases where you have the exact same string within double quotes outside the double quotes.






share|improve this answer































    1














    How about this:



    b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """

    for str_match in re.findall(r"".*?"",b):
    b = re.sub(str_match,re.sub(r","," ",str_match),b)

    print(b)


    output: "hello howdy". sample text, text then comes "Another double quotes" '






    share|improve this answer





























      0














      I'm not completely sure if this will match all your requirements, but on the template you offer the following would return what you are looking for.



      result = re.sub('("(?:[^"])*),((?:[^"])*")', r"1 2")





      share|improve this answer























      • Rocky Li's answer is cleaner, tough.
        – Daniel F
        Nov 20 at 16:51











      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%2f53397243%2fregex-find-substring-and-replace-characters-and-update-whole-string%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      5 Answers
      5






      active

      oldest

      votes








      5 Answers
      5






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Use regex capture groups:



      import re
      s= 'this is, not good "type of ,question" to ask, on stackoverflow'
      re.sub(r'(".*?),(.*?")', r'12', s)


      output:



      'this is, not good "type of question" to ask, on stackoverflow'


      Explanation: (stuff) in regex stands for capture groups, 1 and 2 respectively substitutes the part before and after the , character within the quoted part of string. Please note this also works for multiple quotes within a single string as well.






      share|improve this answer























      • this one worked perfectly for me. Thanks for helping me out @Rocky Li
        – Devanshu Khokhani
        Nov 20 at 16:34










      • Glad to be able to help
        – Rocky Li
        Nov 20 at 16:34










      • Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
        – Daniel F
        Nov 20 at 16:53






      • 1




        This would only remove one instance of comma though, which is fair if that's all OP need.
        – Idlehands
        Nov 20 at 16:57












      • Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
        – Daniel F
        Nov 20 at 16:59


















      4














      Use regex capture groups:



      import re
      s= 'this is, not good "type of ,question" to ask, on stackoverflow'
      re.sub(r'(".*?),(.*?")', r'12', s)


      output:



      'this is, not good "type of question" to ask, on stackoverflow'


      Explanation: (stuff) in regex stands for capture groups, 1 and 2 respectively substitutes the part before and after the , character within the quoted part of string. Please note this also works for multiple quotes within a single string as well.






      share|improve this answer























      • this one worked perfectly for me. Thanks for helping me out @Rocky Li
        – Devanshu Khokhani
        Nov 20 at 16:34










      • Glad to be able to help
        – Rocky Li
        Nov 20 at 16:34










      • Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
        – Daniel F
        Nov 20 at 16:53






      • 1




        This would only remove one instance of comma though, which is fair if that's all OP need.
        – Idlehands
        Nov 20 at 16:57












      • Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
        – Daniel F
        Nov 20 at 16:59
















      4












      4








      4






      Use regex capture groups:



      import re
      s= 'this is, not good "type of ,question" to ask, on stackoverflow'
      re.sub(r'(".*?),(.*?")', r'12', s)


      output:



      'this is, not good "type of question" to ask, on stackoverflow'


      Explanation: (stuff) in regex stands for capture groups, 1 and 2 respectively substitutes the part before and after the , character within the quoted part of string. Please note this also works for multiple quotes within a single string as well.






      share|improve this answer














      Use regex capture groups:



      import re
      s= 'this is, not good "type of ,question" to ask, on stackoverflow'
      re.sub(r'(".*?),(.*?")', r'12', s)


      output:



      'this is, not good "type of question" to ask, on stackoverflow'


      Explanation: (stuff) in regex stands for capture groups, 1 and 2 respectively substitutes the part before and after the , character within the quoted part of string. Please note this also works for multiple quotes within a single string as well.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 20 at 16:39

























      answered Nov 20 at 16:29









      Rocky Li

      2,8101316




      2,8101316












      • this one worked perfectly for me. Thanks for helping me out @Rocky Li
        – Devanshu Khokhani
        Nov 20 at 16:34










      • Glad to be able to help
        – Rocky Li
        Nov 20 at 16:34










      • Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
        – Daniel F
        Nov 20 at 16:53






      • 1




        This would only remove one instance of comma though, which is fair if that's all OP need.
        – Idlehands
        Nov 20 at 16:57












      • Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
        – Daniel F
        Nov 20 at 16:59




















      • this one worked perfectly for me. Thanks for helping me out @Rocky Li
        – Devanshu Khokhani
        Nov 20 at 16:34










      • Glad to be able to help
        – Rocky Li
        Nov 20 at 16:34










      • Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
        – Daniel F
        Nov 20 at 16:53






      • 1




        This would only remove one instance of comma though, which is fair if that's all OP need.
        – Idlehands
        Nov 20 at 16:57












      • Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
        – Daniel F
        Nov 20 at 16:59


















      this one worked perfectly for me. Thanks for helping me out @Rocky Li
      – Devanshu Khokhani
      Nov 20 at 16:34




      this one worked perfectly for me. Thanks for helping me out @Rocky Li
      – Devanshu Khokhani
      Nov 20 at 16:34












      Glad to be able to help
      – Rocky Li
      Nov 20 at 16:34




      Glad to be able to help
      – Rocky Li
      Nov 20 at 16:34












      Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
      – Daniel F
      Nov 20 at 16:53




      Very nice. In order to add the space (from the requirement replace ',' with ' '), add a space between 1 and 2
      – Daniel F
      Nov 20 at 16:53




      1




      1




      This would only remove one instance of comma though, which is fair if that's all OP need.
      – Idlehands
      Nov 20 at 16:57






      This would only remove one instance of comma though, which is fair if that's all OP need.
      – Idlehands
      Nov 20 at 16:57














      Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
      – Daniel F
      Nov 20 at 16:59






      Also unbalanced quotes will introduced problems. See 'this is, not good", "type of ,question" to ask, on stackoverflow' yields this is, not good" "type of ,question" to ask, on stackoverflow
      – Daniel F
      Nov 20 at 16:59















      2














      Another way that gives you some flexibility is you can do it by two steps:




      1. Find all the matches that are contained in quotations,


      2. In each match look for and replace the ','.



      Example:



      # define a pattern that gets you everything inside a double quote
      pat = re.compile(r'"[^"]+"')

      # re.sub the quote pattern and replace the , in each of those matches.
      string = pat.sub(lambda x: x.group(0).replace(',',''), string)

      # 'this is, not good "type of question" to ask, on stackoverflow'


      The flexibility of this is it allows you to replace as many ',' as you need, and you can perform other changes as well once you have located all the double quote patterns.






      share|improve this answer





















      • This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
        – Rocky Li
        Nov 20 at 17:14
















      2














      Another way that gives you some flexibility is you can do it by two steps:




      1. Find all the matches that are contained in quotations,


      2. In each match look for and replace the ','.



      Example:



      # define a pattern that gets you everything inside a double quote
      pat = re.compile(r'"[^"]+"')

      # re.sub the quote pattern and replace the , in each of those matches.
      string = pat.sub(lambda x: x.group(0).replace(',',''), string)

      # 'this is, not good "type of question" to ask, on stackoverflow'


      The flexibility of this is it allows you to replace as many ',' as you need, and you can perform other changes as well once you have located all the double quote patterns.






      share|improve this answer





















      • This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
        – Rocky Li
        Nov 20 at 17:14














      2












      2








      2






      Another way that gives you some flexibility is you can do it by two steps:




      1. Find all the matches that are contained in quotations,


      2. In each match look for and replace the ','.



      Example:



      # define a pattern that gets you everything inside a double quote
      pat = re.compile(r'"[^"]+"')

      # re.sub the quote pattern and replace the , in each of those matches.
      string = pat.sub(lambda x: x.group(0).replace(',',''), string)

      # 'this is, not good "type of question" to ask, on stackoverflow'


      The flexibility of this is it allows you to replace as many ',' as you need, and you can perform other changes as well once you have located all the double quote patterns.






      share|improve this answer












      Another way that gives you some flexibility is you can do it by two steps:




      1. Find all the matches that are contained in quotations,


      2. In each match look for and replace the ','.



      Example:



      # define a pattern that gets you everything inside a double quote
      pat = re.compile(r'"[^"]+"')

      # re.sub the quote pattern and replace the , in each of those matches.
      string = pat.sub(lambda x: x.group(0).replace(',',''), string)

      # 'this is, not good "type of question" to ask, on stackoverflow'


      The flexibility of this is it allows you to replace as many ',' as you need, and you can perform other changes as well once you have located all the double quote patterns.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 at 16:38









      Idlehands

      3,9871417




      3,9871417












      • This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
        – Rocky Li
        Nov 20 at 17:14


















      • This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
        – Rocky Li
        Nov 20 at 17:14
















      This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
      – Rocky Li
      Nov 20 at 17:14




      This should be higher up. My solution does not address multiple , in a single quote, which is a big overlook on my part.
      – Rocky Li
      Nov 20 at 17:14











      1














      How about a combination of split() and replace()? :



      s = 'this is, not good "type of ,question" to ask, on stackoverflow'

      splitted = s.split('"')
      print(s.replace(splitted[1], splitted[1].replace(',', '')))

      # this is, not good "type of question" to ask, on stackoverflow


      Note: This works in this case, but does not work in cases where you have the exact same string within double quotes outside the double quotes.






      share|improve this answer




























        1














        How about a combination of split() and replace()? :



        s = 'this is, not good "type of ,question" to ask, on stackoverflow'

        splitted = s.split('"')
        print(s.replace(splitted[1], splitted[1].replace(',', '')))

        # this is, not good "type of question" to ask, on stackoverflow


        Note: This works in this case, but does not work in cases where you have the exact same string within double quotes outside the double quotes.






        share|improve this answer


























          1












          1








          1






          How about a combination of split() and replace()? :



          s = 'this is, not good "type of ,question" to ask, on stackoverflow'

          splitted = s.split('"')
          print(s.replace(splitted[1], splitted[1].replace(',', '')))

          # this is, not good "type of question" to ask, on stackoverflow


          Note: This works in this case, but does not work in cases where you have the exact same string within double quotes outside the double quotes.






          share|improve this answer














          How about a combination of split() and replace()? :



          s = 'this is, not good "type of ,question" to ask, on stackoverflow'

          splitted = s.split('"')
          print(s.replace(splitted[1], splitted[1].replace(',', '')))

          # this is, not good "type of question" to ask, on stackoverflow


          Note: This works in this case, but does not work in cases where you have the exact same string within double quotes outside the double quotes.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 16:45

























          answered Nov 20 at 16:32









          Austin

          9,4423828




          9,4423828























              1














              How about this:



              b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """

              for str_match in re.findall(r"".*?"",b):
              b = re.sub(str_match,re.sub(r","," ",str_match),b)

              print(b)


              output: "hello howdy". sample text, text then comes "Another double quotes" '






              share|improve this answer


























                1














                How about this:



                b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """

                for str_match in re.findall(r"".*?"",b):
                b = re.sub(str_match,re.sub(r","," ",str_match),b)

                print(b)


                output: "hello howdy". sample text, text then comes "Another double quotes" '






                share|improve this answer
























                  1












                  1








                  1






                  How about this:



                  b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """

                  for str_match in re.findall(r"".*?"",b):
                  b = re.sub(str_match,re.sub(r","," ",str_match),b)

                  print(b)


                  output: "hello howdy". sample text, text then comes "Another double quotes" '






                  share|improve this answer












                  How about this:



                  b=""" "hello, howdy". sample text, text then comes "Another, double, quotes" """

                  for str_match in re.findall(r"".*?"",b):
                  b = re.sub(str_match,re.sub(r","," ",str_match),b)

                  print(b)


                  output: "hello howdy". sample text, text then comes "Another double quotes" '







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 at 17:39









                  Akhilesha

                  113




                  113























                      0














                      I'm not completely sure if this will match all your requirements, but on the template you offer the following would return what you are looking for.



                      result = re.sub('("(?:[^"])*),((?:[^"])*")', r"1 2")





                      share|improve this answer























                      • Rocky Li's answer is cleaner, tough.
                        – Daniel F
                        Nov 20 at 16:51
















                      0














                      I'm not completely sure if this will match all your requirements, but on the template you offer the following would return what you are looking for.



                      result = re.sub('("(?:[^"])*),((?:[^"])*")', r"1 2")





                      share|improve this answer























                      • Rocky Li's answer is cleaner, tough.
                        – Daniel F
                        Nov 20 at 16:51














                      0












                      0








                      0






                      I'm not completely sure if this will match all your requirements, but on the template you offer the following would return what you are looking for.



                      result = re.sub('("(?:[^"])*),((?:[^"])*")', r"1 2")





                      share|improve this answer














                      I'm not completely sure if this will match all your requirements, but on the template you offer the following would return what you are looking for.



                      result = re.sub('("(?:[^"])*),((?:[^"])*")', r"1 2")






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 20 at 16:52

























                      answered Nov 20 at 16:44









                      Daniel F

                      6,24944864




                      6,24944864












                      • Rocky Li's answer is cleaner, tough.
                        – Daniel F
                        Nov 20 at 16:51


















                      • Rocky Li's answer is cleaner, tough.
                        – Daniel F
                        Nov 20 at 16:51
















                      Rocky Li's answer is cleaner, tough.
                      – Daniel F
                      Nov 20 at 16:51




                      Rocky Li's answer is cleaner, tough.
                      – Daniel F
                      Nov 20 at 16:51


















                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53397243%2fregex-find-substring-and-replace-characters-and-update-whole-string%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

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin