Remove multiple punctuations scattered throughout string












2















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:]])'









share|improve this question



























    2















    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:]])'









    share|improve this question

























      2












      2








      2








      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:]])'









      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 1:33









      qq3254qq3254

      508




      508
























          1 Answer
          1






          active

          oldest

          votes


















          3














          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.






          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%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









            3














            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.






            share|improve this answer




























              3














              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.






              share|improve this answer


























                3












                3








                3







                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.






                share|improve this answer













                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 1:38









                Julius VainoraJulius Vainora

                38k76584




                38k76584
































                    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%2f53491515%2fremove-multiple-punctuations-scattered-throughout-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

                    Lallio

                    Unable to find Lightning Node

                    Futebolista