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 dart flutter
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 dart flutter
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 dart flutter
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
regex dart flutter
edited Nov 10 at 15:03
CopsOnRoad
2,1671817
2,1671817
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
New contributor
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'...'
)
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
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
New contributor
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
New contributor
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
New contributor
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
New contributor
edited Nov 10 at 16:33
New contributor
answered Nov 10 at 15:06
filleduchaos
692
692
New contributor
New contributor
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'...'
)
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
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'...'
)
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
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'...'
)
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
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
answered Nov 10 at 15:58
Wiktor Stribiżew
299k16121195
299k16121195
add a comment |
add a comment |
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
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
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
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
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