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,41011018




      2,41011018










      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






















          • 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






















            • 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






















            • 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














            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














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 16:33

























            answered Nov 10 at 15:06









            filleduchaos

            792




            792











            • 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

                304k16123200




                304k16123200



























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







                    Popular posts from this blog

                    Kleinkühnau

                    Makov (Slowakei)

                    Deutsches Schauspielhaus