-
October 13th, 2011, 06:44 AM
#1
Regex except words
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?
-
October 14th, 2011, 03:00 AM
#2
Re: Regex except 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
Last edited by ricky_cpp; October 14th, 2011 at 03:04 AM.
-
October 14th, 2011, 05:46 AM
#3
Re: Regex except words
I personally wouldn't use a regex for this.
Try this :
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);
Darwen.
-
October 14th, 2011, 06:21 AM
#4
Re: Regex except words
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.
-
October 14th, 2011, 07:33 AM
#5
Re: Regex except words
Have you looked at string.Replace method ?
e.g.
Code:
string x = "AT52156123156\r\nNL648312315";
string result = x.Replace("AT", string.Empty);
result = result.Replace("NL", string.Empty);
Just call it multiple times for each string you want removed.
Darwen.
-
October 14th, 2011, 09:07 AM
#6
Re: Regex except words
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);
Last edited by ricky_cpp; October 14th, 2011 at 09:09 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|