15,848,453 members
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

## Solution 2

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

v2
Thomas Daniels 12-Dec-14 10:33am
Andreas Gieriet 12-Dec-14 18:46pm
Agreed! My 5 too!
Cheers
Andi

## Solution 4

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```

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 ;)

## Solution 6

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

v2
Maciej Los 13-Dec-14 4:16am
5ed!
Andreas Gieriet 13-Dec-14 7:16am
Cheers
Andi

## Solution 1

Member 11279092 2-Dec-14 4:04am
i didnt get any relative answer to this question after seeing 30mins regex tutorial

## Solution 3

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.

## Solution 5

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```