找一个字符串应该有一个特定的字母? [英] Finding a string should have a particular letters?
本文介绍了找一个字符串应该有一个特定的字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有多个名字。
我想找一个名字应该有一个字母a,n,u
这3个字母必须出现在这些名字中。
我怎样才能找到它?
例子
1.Anitha
2.Nandu
3.anushka
4.jayasree
解决方案
参见这个正则表达式:
/ ^(?= [^ a] * a)( ?= [^ n] * n)(?= [^ u] * u)/ i
正则表达式解释:
^
断言字符串开头的位置。
(?= [^ a] * a)
断言<$所有非< - b> a
a > s。
(?= [^ n] * n)
断言所有非< - b> n $ c后
n
$ c> s。
(?= [^ u] * u
断言u 所有
出现。非 - u
s后,
/ i
使模式不区分大小写。
要更独特地使用前瞻,你可以添加另一个(?=。*< to_assert>)
组。整个正则表达式可以简化为:
/ ^(?= [^ a ] * a)(?= [^ n] * n)[^ u] * u / i
< a href =https://regex101.com/r/hS8aC1/1>这是一个正则表达式演示。
了解更多: Stack Overflow - Regex存在一些其顺序无关紧要的单词
使用 Linq [ ^ ]查询:
string [] words = new string [ ] { Anitha, < span class =code-string> Nandu, anushka, jayasree 跨度>};
var qry = 来自 w in 字
其中 w.ToLower()。包含(' < span class =code-string> a')&& w.ToLower()。包含(' n')&& w.ToLower()。包含(' u')
选择 w;
foreach ( string word in qry)
{
Console.WriteLine(word);
}
结果:
Nandu
anushka
这个怎么样 - 不是RegEx,但也产生了预期的结果:
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()
;
干杯
Andi
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
解决方案
See this 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 thenon-"a"
s.
(?=[^n]*n)
Asserts that"n"
is present after all thenon-"n"
s.
(?=[^u]*u
Asserts that"u"
is present after all thenon-"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:
/^(?=[^a]*a)(?=[^n]*n)[^u]*u/i
Read more: Stack Overflow - Regex for existence of some words whose order doesn't matter
The same you can achieve using Linq[^] query:
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
How about this - not RegEx, but produces also the desired result:
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
这篇关于找一个字符串应该有一个特定的字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文