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
regex
add a comment |
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
regex
add a comment |
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
regex
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
regex
edited Nov 10 at 15:03
CopsOnRoad
2,41011018
2,41011018
asked Nov 10 at 13:59
ketiwu
204
204
add a comment |
add a comment |
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
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
add a comment |
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'...')Wregex 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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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'...')Wregex 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
add a comment |
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'...')Wregex 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
add a comment |
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'...')Wregex 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
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'...')Wregex 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
answered Nov 10 at 15:58
Wiktor Stribiżew
304k16123200
304k16123200
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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