Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple names.

I want to find a names should have a letters "a,n,u"

These 3 letters must be present in those names.

How can i find it?

Examples

1.Anitha

2.Nandu

3.anushka

4.jayasree
Posted

See this regex:

RegEx
/^(?=[^a]*a)(?=[^n]*n)(?=[^u]*u)/i


Regex explanation:

  • ^ Asserts position at start of string.

  • (?=[^a]*a) Asserts that "a" is present after all the non-"a"s.

  • (?=[^n]*n) Asserts that "n" is present after all the non-"n"s.

  • (?=[^u]*u Asserts that "u" is present after all the non-"u"s.


  • /i Makes the pattern case-insensitive.


To use lookaheads more exclusively, you can add another (?=.*<to_assert>) group. The entire regex can be simplified as:

RegEx
/^(?=[^a]*a)(?=[^n]*n)[^u]*u/i


Here is a regex demo.


Read more: Stack Overflow - Regex for existence of some words whose order doesn't matter
 
Share this answer
 
v2
Comments
Thomas Daniels 12-Dec-14 10:33am    
+5, nice answer!
Andreas Gieriet 12-Dec-14 18:46pm    
Agreed! My 5 too!
Cheers
Andi
The same you can achieve using Linq[^] query:
C#
string[] words = new string[]{"Anitha", "Nandu","anushka","jayasree"};

var qry = from w in words
        where w.ToLower().Contains('a') && w.ToLower().Contains('n') && w.ToLower().Contains('u')
        select w;

foreach(string word in qry)
{
    Console.WriteLine(word);
}


Result:
Nandu
anushka
 
Share this answer
 
Comments
Andreas Gieriet 12-Dec-14 18:44pm    
My 5. Some comment: you might use ...let s = w.ToLower() where s.Contains('a') && s....
Cheers
Andi
Maciej Los 13-Dec-14 4:14am    
Good point! I have done it that way originally, but later... i wanted to display input string.
Thak you, Andi ;)
Andreas Gieriet 13-Dec-14 7:15am    
You can still select the original string. The "let" simply defines a "local variable" in the query.
Cheers
Andi
Maciej Los 13-Dec-14 8:22am    
Good to know ;)
How about this - not RegEx, but produces also the desired result:
C#
var names = new string[]
{ "Anitha"
, "Nandu"
, "anushka"
, "jayasree"
};
string[] anu = names.Where(n=>n.IndexOfAny("aA")>=0)
                    .Where(n=>n.IndexOfAny("nN")>=0)
                    .Where(n=>n.IndexOfAny("uU")>=0)
                    .ToArray()
                    ;

Cheers
Andi
 
Share this answer
 
v2
Comments
Maciej Los 13-Dec-14 4:16am    
5ed!
Andreas Gieriet 13-Dec-14 7:16am    
Thanks for your 5!
Cheers
Andi
 
Share this answer
 
Comments
Member 11279092 2-Dec-14 4:04am    
i didnt get any relative answer to this question after seeing 30mins regex tutorial
Your requirement is not totally clear.
A. Do you just want to find any string that contains at least 1 each of a, n, u?
B. Do they have to be in the order of something, then 'a', then later an 'n', then later a 'u'?
Of your 4 examples, it appears that 1 and 4 should fail, and 2 and 3 should pass.
Right?
For the case A above:
(?:a.*n.*u)|(?:a.*u.*n)|(?:n.*a.*u)|(?:n.*u.*a)|(?:u.*n.*a)|(?:u.*a.*n)

For case B:
(?:a.*n.*u)

The / delimiters and i at the end for case insensitive depend on the context where you are using this. For example, these would be appropriate for a Perl script, but not for a .NET Regex.
 
Share this answer
 
I would suggest two possible solutions:

1) Using regex: (using positive lookahead)

Dim names As New List(Of String)(New String() {"Anushka", "Anitha", "Nandu", "jayasree"})

'Solution 1
For Each name As String In names
    Debug.Print(System.Text.RegularExpressions.Regex.Match(name, "^(?=.*a)(?=.*n)(?=.*u).*$").ToString())
Next


2) Using loops and Lists:

Dim names As New List(Of String)(New String() {"Anushka", "Anitha", "Nandu", "jayasree"})

'Solution 2
Dim mustHaveChars As New List(Of Char)(New Char() {"a"c, "n"c, "u"c})
For Each name As String In names
   Dim n As New List(Of Char)(name.ToCharArray().Distinct().ToArray())
   If n.Except(mustHaveChars).Count = n.Count - mustHaveChars.Count Then
      Debug.Print(name)
   End If
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900