我有以下字符串,我希望所有路径都使用正则表达式
<plugin_output>
Path : C:\Program Files\McAfee\Endpoint Security\
</plugin_output>
<plugin_output>
KB : 4346087
- C:\Windows\system32\mcupdate_genuineintel.dll text here to be ignored
</plugin_output>
<plugin_output>
The following instances of Java are installed on the
remote host :
Path : D:\apps\webtek\oracle\redhat_jdk8u242\\jre\
Path : D:\apps\webtek\oracle\redhat_jdk8u242\jre\
Path : D:\apps\webtek\oracle\redhat_jdk8u242\jre
</plugin_output>
我可以使用这个提取大部分路径:
([A-Z]:)?\\.*\.*
但我会提取所有路径。
答案 0 :(得分:0)
正则表达式是强制性的吗?如果不是,那么这对于这种特定类型的输入应该是有效的:
public static string[] GetPaths(string text)
{
List<string> paths = new List<string>();
using (StringReader reader = new StringReader(text))
{
while (reader.Peek() != -1)
{
string line = reader.ReadLine();
if (line.TrimStart().StartsWith("Path") && line.Contains(":"))
{
paths.Add(line.Substring(line.IndexOf(":") + 1).Trim());
}
}
}
return paths.ToArray();
}