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, " ", ".,!?:;()'" );









share|improve this question
























  • 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




    What is the desired result string, please?
    – Dmitry Bychenko
    Nov 21 at 13:10















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, " ", ".,!?:;()'" );









share|improve this question
























  • 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




    What is the desired result string, please?
    – Dmitry Bychenko
    Nov 21 at 13:10













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, " ", ".,!?:;()'" );









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




    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






  • 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












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);





share|improve this answer





















  • Thanks, works perfectly...
    – Omegand
    Nov 22 at 22:15


















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,}", " ");
}





share|improve this answer





















  • Thanks for the mini-lesson, was a big help.
    – Omegand
    Nov 22 at 22:16











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%2f53412239%2fremove-a-string-word-seperator-with-regex%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
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);





share|improve this answer





















  • Thanks, works perfectly...
    – Omegand
    Nov 22 at 22:15















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);





share|improve this answer





















  • Thanks, works perfectly...
    – Omegand
    Nov 22 at 22:15













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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 12:57









Mikitori

479616




479616












  • 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




Thanks, works perfectly...
– Omegand
Nov 22 at 22:15












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,}", " ");
}





share|improve this answer





















  • Thanks for the mini-lesson, was a big help.
    – Omegand
    Nov 22 at 22:16















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,}", " ");
}





share|improve this answer





















  • Thanks for the mini-lesson, was a big help.
    – Omegand
    Nov 22 at 22:16













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,}", " ");
}





share|improve this answer












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,}", " ");
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)