user3998574
user3998574

Reputation:

Regex : match word except specified word

How do I use regex to match any word without show specified word

i want eliminate the word "you"

i have some word for example :

you eat
you handsome
you die
you will
you lie
and others

so, this program result :

eat
handsome
die
will
lie

Upvotes: 0

Views: 183

Answers (6)

user3998574
user3998574

Reputation:

how when this problem like this ?

are you serious ?
are you ?
you are greatfull

and result :

are serious ?
are ?
are greatefull

Upvotes: 0

netblognet
netblognet

Reputation: 2026

Try this:

String sourcestring = "source string to match with pattern";
Regex re = new Regex(@"^you (.*)/$",RegexOptions.Multiline |
                                    RegexOptions.Singleline);
MatchCollection mc = re.Matches(sourcestring);
int mIdx=0;
foreach (Match m in mc)
{
   for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
   {
       Console.WriteLine("[{0}][{1}] = {2}", mIdx, 
                         re.GetGroupNames()[gIdx], 
                         m.Groups[gIdx].Value);
   }
   mIdx++;
}

But you don't need Regex.

string[] words = new string[] { "you eat",
                   "you handsome",
                   "you die",
                   "you will",
                   "you lie",
                   "and others" };
foreach (var word in words)
{
     var result = word.Replace("you", "");
}

Upvotes: 1

Federico Piazza
Federico Piazza

Reputation: 31035

PCRE approach

If you use PCRE (perl compatible regex) you can leverage the skip/fail flags like this:

you(*SKIP)(*FAIL)|\b(\w+)\b

Working demo

enter image description here

Then whether you access to the capturing group you can grab:

MATCH 1
1.  [4-7]   `eat`
MATCH 2
1.  [12-20] `handsome`
MATCH 3
1.  [25-28] `die`
MATCH 4
1.  [33-37] `will`
MATCH 5
1.  [42-45] `lie`
MATCH 6
1.  [46-49] `and`
MATCH 7
1.  [50-56] `others`

Quoting a paragraph of regular-expressions.info

PCRE is short for Perl Compatible Regular Expressions. It is the name of an open source library written in C by Phillip Hazel. The library is compatible with a great number of C compilers and operating systems. Many people have derived libraries from PCRE to make it compatible with other programming languages. The regex features included with PHP, Delphi, and R, and Xojo (REALbasic) are all based on PCRE. The library is also included with many Linux distributions as a shared .so library and a .h header file.

Discard technique approach

On the ther hand, if you aren't using pcre regex then you can use an excellent regex technique (the best in my opinion) commonly named discard technique. It consists of matching all the patterns you don't want using a chain of OR and at the end of the chain use the pattern you are interested in and capture it:

discard patt1 | discard patt2 | discard pattN |(grab this!)

For your case, you can use:

you|\b(\w+)\b
 ^       ^--- Capture this    
 +-- Discard 'you'

Upvotes: 3

slartidan
slartidan

Reputation: 21608

If you want to use regular expressions, than this will work if you replace it with matcher group $2:

/(you (\w+))|(\w+ \w+)/g

http://regexr.com/3a8uk

Upvotes: 1

Paystey
Paystey

Reputation: 3242

/you (.*)/

The matching group will be everything except the you at the beginning.

Upvotes: -1

AMDcze
AMDcze

Reputation: 526

Use word boundaries and negative lookahead:

\b(?!you\b)\w+\b

Upvotes: 1

Related Questions