Hello,
I have a problem with Regex in c# pattern.
I want to except set of chars(words) from searching in text. This syntax [^abc] i delete each a, b and c from my search but not a whole word "abc". How can I do it to delete a several words?
Printable View
Hello,
I have a problem with Regex in c# pattern.
I want to except set of chars(words) from searching in text. This syntax [^abc] i delete each a, b and c from my search but not a whole word "abc". How can I do it to delete a several words?
Hi,
do not know what exactly you want to achieve.
But if abc is to match only "abc" then the string pattern should be "^abc$"
If it has to match abcd then "^abc"
In both the cases it will not match a, b, c.
Remove [] from your pattern.
In order to delete, just replace your pattern with "". use Regex.Replace(,,) method
Ciao
I personally wouldn't use a regex for this.
Try this :
Darwen.Code:using System;
using System.Linq;
// ...
string test = "abc hello there abc this abcxx isabc a test abc";
string x = string.Join(" ", test.Split(' ').Where(i => i != "abc"));
System.Console.WriteLine(x);
I have a few string's like this in string's array:
AT52156123156
AT5643215231
5632156AT564
2315655153AT
NL648312315
6513N21L56NL
34N534AFVNL
And I want to remove every "AT" and "NL" (and in diffrent case other BE, DE). I want to remove exactly "AT" and "NL" not every A or T or N or L, but a whole set of word. How to use Regex?
"^abc$" - this pattern won't work in middle of string's.
Have you looked at string.Replace method ?
e.g.
Just call it multiple times for each string you want removed.Code:string x = "AT52156123156\r\nNL648312315";
string result = x.Replace("AT", string.Empty);
result = result.Replace("NL", string.Empty);
Darwen.
Yes Foxc it won't work if you do make effort to read a manual or press F1 for the function help
$ sign specifies that the match should end at the last chracter of string and specifies that the match must start at the beginning of a string
Regex.Replace(str, @"(AT)*(NL)*", string.Empty);