This question already has answers here:
Why \b does not match word using .net regex
(2 answers)
Closed 5 years ago.
I want to replace certain words in one sentence string with regex replace.
For it, I create pattern array :
string[] words = {"abc","132","qwe","bold","test"};
and for replace, I do it :
foreach (string item in words){
output = Regex.Replace(output,#"\b" + item + "\b", " ");
}
but this way don't work ...
Someone has an idea?
Explanation
I use the above method in VB.net and will respond without problems.
I am a beginner in C #
It looks like you forgot the # literal at the second \b:
either put it in there
output = Regex.Replace(output, #"\b" + item + #"\b", " ");
or double the backslash so it is used as an escape character:
output = Regex.Replace(output, #"\b" + item + "\\b", " ");
Related
This question already has answers here:
How to split by multiple strings in C#
(4 answers)
splitting a string based on multiple char delimiters
(7 answers)
Closed 3 months ago.
Let's say i have:
string text = "Ivan - George ; Josh , Mike John";
And i want to split it by
" - ", " ; " , " ".
Normally when I want to split a text by 1 element I would use:
List<string> names = text.Split(" - ").ToList();
But now I encountered a problem where i need to split a text by multiple characters and I don't know how to do it...
List<string> names = text.Split(
new []{" - ", " ; ", " "},
StringSplitOptions.None).ToList();
This question already has answers here:
RegEx Match multiple times in string
(4 answers)
Closed 3 years ago.
I'm new to regex and I don't seem te find my way out with those patterns. I'm trying to match the punctuation in a sentence (quotation marks and question mark) with no success.
Here is my code:
string sentence = "\"This is the end?\"";
string punctuation = Regex.Match(sentence, "[\"?]").Value;
What am I doing wrong here? I'd expect the console to display "?", however, it shows me a double quote.
If you want to match all quotation marks and question marks as your question states, then your pattern is okay. The problem is that Regex.Match will only return the first match it finds. From MSDN:
Searches the input string for the first occurrence of the specified regular expression...
You probably want to use Matches:
string sentence = "\"This is the end?\"";
MatchCollection allPunctuation = Regex.Matches(sentence, "[\"?]");
foreach(Match punctuation in allPunctuation)
{
Console.WriteLine("Found {0} at position {1}", punctuation.Value, punctuation.Index);
}
This will return:
Found " at position 0
Found ? at position 16
Found " at position 17
I'd also point out that if you truly want to match all punctuation characters, including things like 'French' quotes (« and »), 'smart' quotes (“ and ”), inverted question marks (¿), and many others, you can use Unicode Character categories with a pattern like \p{P}.
You need to call Matches instead of Match.
Example:
string sentence = "\"This is the end?\"";
var matches = Regex.Matches(sentence, "[\"?]");
var punctuationLocations = string.Empty;
foreach(Match match in matches)
{
punctuationLocations += match.Value + " at index:" + match.Index + Environment.NewLine;
}
// punctuationLocations:
// " at index:0
// ? at index:16
// " at index:17
This question already has answers here:
What characters need to be escaped in .NET Regex?
(4 answers)
Closed 7 years ago.
I use dynamically built regex. Problem is when symbol = "aaaa (1)" because regex tries to parse it, but I want to treat it literary
Regex regex = new Regex(#"(^" + "/(" + symbol + #" \(\d+\)$)|" + symbol);
You need to escape special chars:
var escapedSymbol = Regex.Escape(symbol);
Regex regex = new Regex(#"(^" + "/(" + escapedSymbol + #" \(\d+\)$)|" + escapedSymbol );
Reffer: msdn
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 7 years ago.
I have a string of different emails
ex: "email1#uy.com, email2#iu.it, email3#uu.edu" etc, etc
I would like to formulate a Regex that creates the following output
ex: "email1,email2,email3" etc, etc
How can I remove characters between an "#" and "," but leaving a "," and a Space in C#
Thank you so much for the help!!
If you want to replace all characters between # and comma by blank, the easiest option is to use Regex.Replace:
var emails = "a#m.com, b#m.com, d#m.com";
var result = Regex.Replace(emails, "#[^,]+", string.Empty);
// result is "a, b, d"
Please note that it leaves spaces after comma in the result, as you wanted in your question, though your example result has spaces removed.
The regular expression looks for all substrings starting '#' characters, followed by any character which is not comma. Those substrings are replaced with empty string.
Replacing all occurrences of #[^,]+ with an empty string will do the job.
The expression matches sequences that start in #, inclusive, up to a comma or to the end, exclusive. Therefore, commas in the original string of e-mails would be kept.
Demo.
Maybe you don't need to use a regex, in that case you can do the following:
string input = "email1#uy.com, email2#iu.it, email3#uu.edu";
input = input.Replace(" ", "");
string[] ocurrences = input.Split(',');
for (int i = 0; i < ocurrences.Length; i++)
{
string s = ocurrences[i];
ocurrences[i] = s.Substring(0, s.IndexOf('#'));
}
string final = string.Join(", ", occurences);
This question already exists:
Closed 10 years ago.
Possible Duplicate:
C# split string but keep split chars / separators
Is there a simple way to do a .Net string split() function that will leave the original split characters in the results?
Such that:
"some text {that|or} another".Split('{','|','}');
would result in an array with:
[0] = "some text "
[1] = "{"
[2] = "that"
[3] = "|"
...
Preferably without a regex.
check out this post
the first answer with a RegEx solution, the second for a non-regex solution...
In Concept...
string source = "123xx456yy789";
foreach (string delimiter in delimiters)
source = source.Replace(delimiter, ";" + delimiter + ";");
string[] parts = source.Split(';');
you can probably roll your own using String.IndexOf Method (String, Int32) to find all of your initial separator characters, and merge those in with the results of String.Split