Remove a string word + seperator with regex
up vote
-2
down vote
favorite
I have a string line
"Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,"
I need to remove a specific word decided with Console.ReadLine() along with the special character that follows it - " .,!?:;()' " from the string line
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
Regex.Replace(text, " ", ".,!?:;()'" );
c# regex string
add a comment |
up vote
-2
down vote
favorite
I have a string line
"Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,"
I need to remove a specific word decided with Console.ReadLine() along with the special character that follows it - " .,!?:;()' " from the string line
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
Regex.Replace(text, " ", ".,!?:;()'" );
c# regex string
Your code won't work, you are not even using theword
anywhere in the code. Also, theRegex.Replace
won't change thetext
, you need to assign the new value totext
.
– Wiktor Stribiżew
Nov 21 at 12:43
1
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have a string line
"Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,"
I need to remove a specific word decided with Console.ReadLine() along with the special character that follows it - " .,!?:;()' " from the string line
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
Regex.Replace(text, " ", ".,!?:;()'" );
c# regex string
I have a string line
"Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,"
I need to remove a specific word decided with Console.ReadLine() along with the special character that follows it - " .,!?:;()' " from the string line
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
Regex.Replace(text, " ", ".,!?:;()'" );
c# regex string
c# regex string
edited Nov 21 at 13:09
Dmitry Bychenko
104k992130
104k992130
asked Nov 21 at 12:40
Omegand
32
32
Your code won't work, you are not even using theword
anywhere in the code. Also, theRegex.Replace
won't change thetext
, you need to assign the new value totext
.
– Wiktor Stribiżew
Nov 21 at 12:43
1
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10
add a comment |
Your code won't work, you are not even using theword
anywhere in the code. Also, theRegex.Replace
won't change thetext
, you need to assign the new value totext
.
– Wiktor Stribiżew
Nov 21 at 12:43
1
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10
Your code won't work, you are not even using the
word
anywhere in the code. Also, the Regex.Replace
won't change the text
, you need to assign the new value to text
.– Wiktor Stribiżew
Nov 21 at 12:43
Your code won't work, you are not even using the
word
anywhere in the code. Also, the Regex.Replace
won't change the text
, you need to assign the new value to text
.– Wiktor Stribiżew
Nov 21 at 12:43
1
1
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
First, to test your regex: https://regex101.com/r/WDOqRc/2
Then the code becomes:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
text = Regex.Replace(text, $"{word}[.,!?:;()']?", String.Empty);
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
add a comment |
up vote
0
down vote
Let's get rid of special characters (I suggest special character is any one except letter, digit or apostroph):
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
// Arvydas g 1964 m gruodžio 19 d Kaune Lietuvos krepšininkas
string result = Regex.Replace(text, @"[W-[']]+", " ");
Then let's remove the word
:
string word = "Kaune";
// removing word
result = Regex
.Replace(result, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
// removing double spaces if any
result = Regex
.Replace(result, "\s{2,}", " ");
Console.WriteLine(result);
Outcome:
Arvydas g 1964 m gruodžio 19 d Lietuvos krepšininkas
Or wrapping it into a routine:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
string text = Regex.Replace(text, @"[W-[']]+", " ");
if (!string.IsNullOrWhiteSpace(word)) {
text = Regex
.Replace(text, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
text = Regex
.Replace(text, "\s{2,}", " ");
}
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
First, to test your regex: https://regex101.com/r/WDOqRc/2
Then the code becomes:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
text = Regex.Replace(text, $"{word}[.,!?:;()']?", String.Empty);
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
add a comment |
up vote
0
down vote
accepted
First, to test your regex: https://regex101.com/r/WDOqRc/2
Then the code becomes:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
text = Regex.Replace(text, $"{word}[.,!?:;()']?", String.Empty);
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
First, to test your regex: https://regex101.com/r/WDOqRc/2
Then the code becomes:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
text = Regex.Replace(text, $"{word}[.,!?:;()']?", String.Empty);
First, to test your regex: https://regex101.com/r/WDOqRc/2
Then the code becomes:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
text = Regex.Replace(text, $"{word}[.,!?:;()']?", String.Empty);
answered Nov 21 at 12:57
Mikitori
479616
479616
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
add a comment |
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
Thanks, works perfectly...
– Omegand
Nov 22 at 22:15
add a comment |
up vote
0
down vote
Let's get rid of special characters (I suggest special character is any one except letter, digit or apostroph):
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
// Arvydas g 1964 m gruodžio 19 d Kaune Lietuvos krepšininkas
string result = Regex.Replace(text, @"[W-[']]+", " ");
Then let's remove the word
:
string word = "Kaune";
// removing word
result = Regex
.Replace(result, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
// removing double spaces if any
result = Regex
.Replace(result, "\s{2,}", " ");
Console.WriteLine(result);
Outcome:
Arvydas g 1964 m gruodžio 19 d Lietuvos krepšininkas
Or wrapping it into a routine:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
string text = Regex.Replace(text, @"[W-[']]+", " ");
if (!string.IsNullOrWhiteSpace(word)) {
text = Regex
.Replace(text, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
text = Regex
.Replace(text, "\s{2,}", " ");
}
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
add a comment |
up vote
0
down vote
Let's get rid of special characters (I suggest special character is any one except letter, digit or apostroph):
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
// Arvydas g 1964 m gruodžio 19 d Kaune Lietuvos krepšininkas
string result = Regex.Replace(text, @"[W-[']]+", " ");
Then let's remove the word
:
string word = "Kaune";
// removing word
result = Regex
.Replace(result, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
// removing double spaces if any
result = Regex
.Replace(result, "\s{2,}", " ");
Console.WriteLine(result);
Outcome:
Arvydas g 1964 m gruodžio 19 d Lietuvos krepšininkas
Or wrapping it into a routine:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
string text = Regex.Replace(text, @"[W-[']]+", " ");
if (!string.IsNullOrWhiteSpace(word)) {
text = Regex
.Replace(text, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
text = Regex
.Replace(text, "\s{2,}", " ");
}
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
add a comment |
up vote
0
down vote
up vote
0
down vote
Let's get rid of special characters (I suggest special character is any one except letter, digit or apostroph):
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
// Arvydas g 1964 m gruodžio 19 d Kaune Lietuvos krepšininkas
string result = Regex.Replace(text, @"[W-[']]+", " ");
Then let's remove the word
:
string word = "Kaune";
// removing word
result = Regex
.Replace(result, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
// removing double spaces if any
result = Regex
.Replace(result, "\s{2,}", " ");
Console.WriteLine(result);
Outcome:
Arvydas g 1964 m gruodžio 19 d Lietuvos krepšininkas
Or wrapping it into a routine:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
string text = Regex.Replace(text, @"[W-[']]+", " ");
if (!string.IsNullOrWhiteSpace(word)) {
text = Regex
.Replace(text, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
text = Regex
.Replace(text, "\s{2,}", " ");
}
Let's get rid of special characters (I suggest special character is any one except letter, digit or apostroph):
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
// Arvydas g 1964 m gruodžio 19 d Kaune Lietuvos krepšininkas
string result = Regex.Replace(text, @"[W-[']]+", " ");
Then let's remove the word
:
string word = "Kaune";
// removing word
result = Regex
.Replace(result, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
// removing double spaces if any
result = Regex
.Replace(result, "\s{2,}", " ");
Console.WriteLine(result);
Outcome:
Arvydas g 1964 m gruodžio 19 d Lietuvos krepšininkas
Or wrapping it into a routine:
string word = Console.ReadLine();
string text = "Arvydas,(g. 1964 m. gruodžio 19 d. Kaune) – Lietuvos krepšininkas,";
string text = Regex.Replace(text, @"[W-[']]+", " ");
if (!string.IsNullOrWhiteSpace(word)) {
text = Regex
.Replace(text, "\b" + Regex.Escape(word) + "\b", " ", RegexOptions.IgnoreCase);
text = Regex
.Replace(text, "\s{2,}", " ");
}
answered Nov 21 at 13:29
Dmitry Bychenko
104k992130
104k992130
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
add a comment |
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
Thanks for the mini-lesson, was a big help.
– Omegand
Nov 22 at 22:16
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412239%2fremove-a-string-word-seperator-with-regex%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
Your code won't work, you are not even using the
word
anywhere in the code. Also, theRegex.Replace
won't change thetext
, you need to assign the new value totext
.– Wiktor Stribiżew
Nov 21 at 12:43
1
What is the desired result string, please?
– Dmitry Bychenko
Nov 21 at 13:10