Chuck Savage
Chuck Savage

Reputation: 11955

regex of any word not preceeded by a period

I've tried several options, but nothing is giving me the whole word.

this gives me partial words,

`@"(?<![.])\w+"`

I'm parsing c# code, so the string "Regex.Match(" ", " ")" should return Regex but not Match.

I've ended up using just \w+ and doing this check,

if ((match.Index > 0) && ('.' == text[match.Index - 1]))
    continue;

Which works fine, but was just curious if there is a regex that would do it as well.

Upvotes: 3

Views: 427

Answers (2)

Asgeir
Asgeir

Reputation: 1112

You could also try: (?<![.])\b\w+\b

Upvotes: 5

Tom Tu
Tom Tu

Reputation: 9593

try this one with zero-width negative lookbehind assertion

(?<!(\.\w*))\w+

what it does is it selects only strings made of only word characters with length of at least one character which aren't preceded by strings of 0 or more word characters preceded by a dot character

more on this and other more tricky regexps in .NET @msdn

Upvotes: 1

Related Questions