Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex.Match double consonants in c#

Tags:

c#

regex

I have a list of many words. What I need is to find all words ending on "ing", "ed","ied" and with a single vowel and doubled consonant before: Should match words: begged, slamming, zagging. Not match helping ("lp"-not double consonant)

\w*[^aoyie][aoyie]([^aoyie])\1(ed|ing|ied)

It's working on RegexPal.com, but it is not working in C# (not matches any words, returns 0 words in list)

My code:

List<Book_to_Word> allWords = (from f in db2.Book_to_Words.AsEnumerable() select f).ToList();

List<Book_to_Word> wordsNOTExist = (from f in allWords
                        where Regex.IsMatch(f.WordStr, @"^(\w*[^aoyie]+[aoyie]([^aoyie])(ed|ing|ied))$")
                                    select f).ToList();

Works when I don't use \1. But returns words with single consonant.

like image 663
Vildan Avatar asked Jun 27 '12 09:06

Vildan


People also ask

How to match() in regex?

Most characters, including all letters ( a-z and A-Z ) and digits ( 0-9 ), match itself. For example, the regex x matches substring "x" ; z matches "z" ; and 9 matches "9" . Non-alphanumeric characters without special meaning in regex also matches itself. For example, = matches "=" ; @ matches "@" .

What is?= in regex?

(?= regex_here) is a positive lookahead. It is a zero-width assertion, meaning that it matches a location that is followed by the regex contained within (?=

What does d mean in regular expression?

From the . NET documentation of Regex , \d matches any decimal digit. The signification of a "decimal digit" depends on the options of the regex: Without RegexOptions. ECMAScript (default): \d means \p{Nd} , e.g. any character from the Unicode category "Decimal digit"

What is regex AZ match?

The regular expression [A-Z][a-z]* matches any sequence of letters that starts with an uppercase letter and is followed by zero or more lowercase letters.


1 Answers

Try to relax the condition a bit:

@"^[a-z]*[aoyie]([^aoyie])\1(ed|ing|ied)$"

Your current regex will require the word to have at least 3 characters before the double consonant and the suffix. So "begged" and "zagging" are not matched.

It is a bit weird, though, to see "y" in the group, while "u" is missing (e.g. "mugged"). You probably want to double check about that. And I have a bit of doubt about double consonant before "ied", but I'll leave it there.

like image 186
nhahtdh Avatar answered Sep 22 '22 16:09

nhahtdh