[英]Searching for a pattern
I need to search a list for some pattern is present or not. 我需要搜索列表中是否存在某种模式。
var result=roles.Where(z=>z.Contains(x) && z.Contains(y)).ToList();
string x = "Resource:resource1:resource2";
string y = "writer";
List<string> roles=new List<string>{"Resource::reader","Resource:resource1::Deleter","Resource:resource1::writer"};
I need to find if any value is present in roles list like: 我需要查找角色列表中是否存在任何值,例如:
Resource::writer
or Resource:resource1::writer
or Resource:resource1:resource2::writer
ie Split x based on : and append y to the combination of splitted x Resource::writer
或Resource:resource1::writer
或Resource:resource1:resource2::writer
即基于:拆分x,并将y附加到拆分的x的组合中
If my understanding of your problem is right : 如果我对您的问题的理解是正确的:
You have a list which can contain anything that you names roles. 您有一个列表,其中可以包含您为角色命名的任何内容。 Thoses roles are in format A::B or A:B::C or A:B:C::D etc...
这些角色的格式为A :: B或A:B :: C或A:B:C :: D等...
And what you want to achieve is to find if any "path" or combination of path from x can give the role y ? 而您想要实现的是找到x的任何“路径”或路径组合是否可以赋予y角色?
for instance : if you have roles like A::ZA::YA:B::XA:B:C::X 例如:如果您具有类似A :: ZA :: YA:B :: XA:B:C :: X的角色
you have x which is A:B:C 你有x是A:B:C
and you have y which is X 你有y就是X
you want to check is you have A::X in the list 您要检查的是列表中是否有A :: X
if you don't, you're gonna check A:B::X in the list, 如果不这样做,您将在列表中检查A:B :: X,
and if you still don't, you will look for A:B:C::X 如果您仍然不这样做,则将查找A:B:C :: X
So again if I'm right, you could consider something like this : 所以如果我没错,您可以考虑这样的事情:
String path = "A:B:C";
String roleNeeded = "X";
List<String> roles = new List<string>() { "A::Z", "A::Y", "A:B::X" };
List<String> pathStep = new List<string>();
pathStep = path.Split(':').ToList();
String lookupPath = String.Empty;
String result = String.Empty;
pathStep.ForEach( s =>
{
lookupPath += s;
if (roles.Contains(lookupPath + "::" + roleNeeded))
{
result = lookupPath + "::" + roleNeeded;
}
lookupPath += ":";
});
if (result != String.Empty)
{
// result is Good_Path::Role
}
This way you start spliting your path X as a list and you aggregate it in the foreach to look at each step. 这样,您便开始将路径X拆分为列表,然后将其汇总到foreach中以查看每个步骤。
You should consider using Regular Expression. 您应该考虑使用正则表达式。 Try this out,
试试看
string x = "Resource:resource1:resource2"; string y = "writer"; List<string> roles; List<string> words = new List<string> { x, y }; // We are using escape to search for multiple strings. string pattern = string.Join("|", words.Select(w => Regex.Escape(w))); Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); // You got matched results... List<string> matchedResults = roles.Where(regex.IsMatch).ToList();
string x = "Resource:resource1:resource2";
string y = "writer";
List<string> roles = new List<string>
{
"Resource::writer",
"Resource:resource1:resource2::writer"
};
var records = x.Split(':').Select((word, index) => new { word, index });
var result =
from record in records
let words = $"{string.Join(":", records.Take(record.index + 1).Select(r => r.word))}::{y}"
join role in roles on words equals role
select words;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.