5

I need to match all the whole words containing a given a string.

string s = "ABC.MYTESTING
XYZ.YOUTESTED
ANY.TESTING";

Regex r = new Regex("(?<TM>[!\..]*TEST.*)", ...);
MatchCollection mc = r.Matches(s);

I need the result to be:

MYTESTING
YOUTESTED
TESTING

But I get:

TESTING
TESTED
.TESTING

How do I achieve this with Regular expressions.

Edit: Extended sample string.

6 Answers 6

4

If you were looking for all words including 'TEST', you should use

@"(?<TM>\w*TEST\w*)"

\w includes word characters and is short for [A-Za-z0-9_]

Sign up to request clarification or add additional context in comments.

2 Comments

@tvr: be aware \w matches [0-9a-zA-Z_]. If you don't want numbers or underscores, stick with \b.
@Brad: It matches a lot more than that, but the important thing is that it doesn't match non-word characters.
2

Keep it simple: why not just try \w*TEST\w* as the match pattern.

Comments

2

I get the results you are expecting with the following:

string s = @"ABC.MYTESTING
XYZ.YOUTESTED
ANY.TESTING";

var m = Regex.Matches(s, @"(\w*TEST\w*)", RegexOptions.IgnoreCase);

3 Comments

+1 for verbatim strings and a (probably) correct regex, but RegexOptions.Multiline serves no purpose here.
@alan Right you are, and now removed. That snuck in from my LINQPad script.
Yeah, RegexBuddy always sneaks that in, too. Very annoying.
1

Try using \b. It's the regex flag for a non-word delimiter. If you wanted to match both words you could use:

/\b[a-z]+\b/i

BTW, .net doesn't need the surrounding /, and the i is just a case-insensitive match flag.

.NET Alternative:

var re = new Regex(@"\b[a-z]+\b", RegexOptions.IgnoreCase);

7 Comments

This matches a 1-letter word, not both words.
Hmm. How do I specify that? I tried this but doesn't work: Regex r = new Regex("\b(?<TM>[!\..]*TEST.*)\b", ...);
@mousino: Indeed i did miss a quantifier, but will match both words.
@tvr: Also, if you want only words starting with "TEST", use \btest[a-z]+\b, e.g. ideone.com/8KNQz
@Brad Thanks for the sample code. This is small part of a larger regular expression and I cannot change now..
|
0

Using Groups I think you can achieve it.

        string s = @"ABC.TESTING
        XYZ.TESTED";
        Regex r = new Regex(@"(?<TM>[!\..]*(?<test>TEST.*))", RegexOptions.Multiline);
        var mc= r.Matches(s);
        foreach (Match match in mc)
        {
            Console.WriteLine(match.Groups["test"]);
        }

Works exactly like you want.

BTW, your regular expression pattern should be a verbatim string ( @"")

3 Comments

The Multiline option isn't needed here, but IgnoreCase might be. And regarding [!\..]*, see my answer.
Yes, but I was just going with the pattern provided by the OP. The other patterns provided are better.
Take it from me: it's never a good idea to use regexes from a question without validating them. Or any code, for that matter. Or from other answers. I've been burned that way too many times. :-/
0
Regex r = new Regex(@"(?<TM>[^.]*TEST.*)", RegexOptions.IgnoreCase);

First, as @manojlds said, you should use verbatim strings for regexes whenever possible. Otherwise you'll have to use two backslashes in most of your regex escape sequences, not just one (e.g. [!\\..]*).

Second, if you want to match anything but a dot, that part of the regex should be [^.]*. ^ is the metacharacter that inverts the character class, not !, and . has no special meaning in that context, so it doesn't need to be escaped. But you should probably use \w* instead, or even [A-Z]*, depending on what exactly you mean by "word". [!\..] matches ! or ..

Regex r = new Regex(@"(?<TM>[A-Z]*TEST[A-Z]*)", RegexOptions.IgnoreCase);

That way you don't need to bother with word boundaries, though they don't hurt:

Regex r = new Regex(@"(?<TM>\b[A-Z]*TEST[A-Z]*\b)", RegexOptions.IgnoreCase);

Finally, if you're always taking the whole match anyway, you don't need to use a capturing group:

Regex r = new Regex(@"\b[A-Z]*TEST[A-Z]*\b", RegexOptions.IgnoreCase);

The matched text will be available via Match's Value property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.