How to remove only symbols from string in dart









up vote
1
down vote

favorite












I want to remove all special symbols from string and have only words in string
I tried this but it gives same output only



main() 
String s = "Hello, world! i am 'foo'";
print(s.replaceAll(new RegExp('W+'),''));



output : Hello, world! i am 'foo'

expected : Hello world i am foo










share|improve this question



























    up vote
    1
    down vote

    favorite












    I want to remove all special symbols from string and have only words in string
    I tried this but it gives same output only



    main() 
    String s = "Hello, world! i am 'foo'";
    print(s.replaceAll(new RegExp('W+'),''));



    output : Hello, world! i am 'foo'

    expected : Hello world i am foo










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to remove all special symbols from string and have only words in string
      I tried this but it gives same output only



      main() 
      String s = "Hello, world! i am 'foo'";
      print(s.replaceAll(new RegExp('W+'),''));



      output : Hello, world! i am 'foo'

      expected : Hello world i am foo










      share|improve this question















      I want to remove all special symbols from string and have only words in string
      I tried this but it gives same output only



      main() 
      String s = "Hello, world! i am 'foo'";
      print(s.replaceAll(new RegExp('W+'),''));



      output : Hello, world! i am 'foo'

      expected : Hello world i am foo







      regex dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 15:03









      CopsOnRoad

      2,1971917




      2,1971917










      asked Nov 10 at 13:59









      ketiwu

      204




      204






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.



          In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^sw]") instead - that matches any character that's not whitespace or a word character






          share|improve this answer










          New contributor




          filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
            – ketiwu
            Nov 10 at 15:32










          • Hey - I updated my answer with a regex that works
            – filleduchaos
            Nov 10 at 16:33

















          up vote
          1
          down vote













          There are two issues:




          • 'W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \, or use a raw string literal (r'...')


          • W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^ws].

          Use



          void main() 
          String s = "Hello, world! i am 'foo'";
          print(s.replaceAll(new RegExp(r'[^ws]+'),''));



          Output: Hello world i am foo






          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',
            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%2f53239702%2fhow-to-remove-only-symbols-from-string-in-dart%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.



            In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^sw]") instead - that matches any character that's not whitespace or a word character






            share|improve this answer










            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
              – ketiwu
              Nov 10 at 15:32










            • Hey - I updated my answer with a regex that works
              – filleduchaos
              Nov 10 at 16:33














            up vote
            1
            down vote



            accepted










            The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.



            In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^sw]") instead - that matches any character that's not whitespace or a word character






            share|improve this answer










            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
              – ketiwu
              Nov 10 at 15:32










            • Hey - I updated my answer with a regex that works
              – filleduchaos
              Nov 10 at 16:33












            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.



            In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^sw]") instead - that matches any character that's not whitespace or a word character






            share|improve this answer










            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            The docs for the RegExp class state that you should use raw strings (a string literal prefixed with an r, like r"Hello world") if you're constructing a regular expression that way. This is particularly necessary where you're using escapes.



            In addition, your regex is going to catch spaces as well, so you'll need to modify that. You can use RegExp(r"[^sw]") instead - that matches any character that's not whitespace or a word character







            share|improve this answer










            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited Nov 10 at 16:33





















            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered Nov 10 at 15:06









            filleduchaos

            692




            692




            New contributor




            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            filleduchaos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.











            • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
              – ketiwu
              Nov 10 at 15:32










            • Hey - I updated my answer with a regex that works
              – filleduchaos
              Nov 10 at 16:33
















            • thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
              – ketiwu
              Nov 10 at 15:32










            • Hey - I updated my answer with a regex that works
              – filleduchaos
              Nov 10 at 16:33















            thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
            – ketiwu
            Nov 10 at 15:32




            thanks that worked but it also removed spaces can you tell me on how to not to remove spaces ?
            – ketiwu
            Nov 10 at 15:32












            Hey - I updated my answer with a regex that works
            – filleduchaos
            Nov 10 at 16:33




            Hey - I updated my answer with a regex that works
            – filleduchaos
            Nov 10 at 16:33












            up vote
            1
            down vote













            There are two issues:




            • 'W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \, or use a raw string literal (r'...')


            • W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^ws].

            Use



            void main() 
            String s = "Hello, world! i am 'foo'";
            print(s.replaceAll(new RegExp(r'[^ws]+'),''));



            Output: Hello world i am foo






            share|improve this answer
























              up vote
              1
              down vote













              There are two issues:




              • 'W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \, or use a raw string literal (r'...')


              • W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^ws].

              Use



              void main() 
              String s = "Hello, world! i am 'foo'";
              print(s.replaceAll(new RegExp(r'[^ws]+'),''));



              Output: Hello world i am foo






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                There are two issues:




                • 'W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \, or use a raw string literal (r'...')


                • W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^ws].

                Use



                void main() 
                String s = "Hello, world! i am 'foo'";
                print(s.replaceAll(new RegExp(r'[^ws]+'),''));



                Output: Hello world i am foo






                share|improve this answer












                There are two issues:




                • 'W' is not a valid escape sequence, to define a backslash in a regular string literal, you need to use \, or use a raw string literal (r'...')


                • W regex pattern matches any char that is not a word char including whitespace, you need to use a negated character class with word and whitespace classes, [^ws].

                Use



                void main() 
                String s = "Hello, world! i am 'foo'";
                print(s.replaceAll(new RegExp(r'[^ws]+'),''));



                Output: Hello world i am foo







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 15:58









                Wiktor Stribiżew

                299k16121195




                299k16121195



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239702%2fhow-to-remove-only-symbols-from-string-in-dart%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







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3