C# 如何从动态字符串C中获取特定字符串#
我在提取特定字符串时遇到问题。我有以下字符串(可能会有所不同):C# 如何从动态字符串C中获取特定字符串#,c#,string,parsing,C#,String,Parsing,我在提取特定字符串时遇到问题。我有以下字符串(可能会有所不同): 显示目录名称:“我的设备名称”,描述:“ASA 5506-X SSD“PID:ASA5506-SSD,视频:不适用,序列号:MSA203301BVWARD-99ST-FW01#名称: “底盘”,说明:“ASA 5506-X,配备火力服务,8GE,AC,DES”PID:ASA5506 ,VID:V06,SN:JMX2042Y12V名称:“存储设备1”,描述:“ASA5506-X SSD” PID:ASA5506-SSD,视频:不适用
显示目录
名称:“我的设备名称”,描述:“ASA 5506-X
SSD“
PID:ASA5506-SSD,视频:不适用,序列号:MSA203301BV
WARD-99ST-FW01#名称:
“底盘”,说明:“ASA 5506-X,配备火力服务,8GE,AC,DES”
PID:ASA5506
,VID:V06,SN:JMX2042Y12V
名称:“存储设备1”,描述:“ASA5506-X SSD”
PID:ASA5506-SSD,视频:不适用,序列号:MSA203301BV
WARD-99ST-FW01#
我想从这个字符串解析“SN”,其中“Name”=机箱。因此,从上面的字符串中,我需要result=“JMX2042Y12V”。我不想使用
标记进行解析,因为在某些情况下它们不会出现
到目前为止,我已经使用了这个:(工作不正常,它基于我不想使用的
标记
private static List<string> ExtractFromBody(string body, string start, string end)
{
List<string> matched = new List<string>();
int indexStart = 0;
int indexEnd = 0;
bool exit = false;
while (!exit)
{
indexStart = body.IndexOf(start);
if (indexStart != -1)
{
indexEnd = indexStart + body.Substring(indexStart).IndexOf(end);
matched.Add(body.Substring(indexStart + start.Length, indexEnd - indexStart - start.Length));
body = body.Substring(indexEnd + end.Length);
}
else
{
exit = true;
}
}
return matched;
}
私有静态列表ExtractFromBody(字符串主体、字符串开始、字符串结束)
{
列表匹配=新列表();
int indexStart=0;
int indexEnd=0;
bool exit=false;
当(!退出)
{
indexStart=body.IndexOf(开始);
如果(indexStart!=-1)
{
indexEnd=indexStart+body.Substring(indexStart.IndexOf(end));
Add(body.Substring(indexStart+start.Length,indexEnd-indexStart-start.Length));
body=body.Substring(indexEnd+end.Length);
}
其他的
{
退出=真;
}
}
返回匹配;
}
我是这样打电话的:
var result= ExtractFromBody(St, ", SN: ", "<br/>");
foreach (var r in result)
{
Console.Write(r);
}
Console.ReadLine();
var result=ExtractFromBody(St,”,SN:“,”
);
foreach(结果中的var)
{
控制台写入(r);
}
Console.ReadLine();
您可能可以使用正则表达式(与查找要搜索的子字符串起始索引的现有代码结合使用)来搜索序列号
使用系统;
使用System.Text.RegularExpressions;
公共课程
{
公共静态void Main()
{
var body=“show inventory
Name:“我的设备名称一”,DESCR:“ASA 5506-XSSD\”
PID:ASA5506-SSD,VID:N/A,SN:MSA203301BV
WARD-99ST-FW01;Name:“Chassis\”,DESCR:“ASA 5506-X带火力服务,8GE,AC,DES\”
PID:ASA5506,VID:V06,SN:JMX2042Y12V
Name:“Storage Device 1\”,DESCR:“ASAR-SSD\”\“
PID:ASA5506-SSD,视频:不适用,序列号:MSA203301BV
WARD-99ST-FW01”;
var regex=新的regex(“(序号:)(\\w+)”);
var indexStart=body.IndexOf(“机箱”);
var substringToParse=body.Substring(indexStart);
var matchingResult=regex.Match(substringToParse);
//序列号:JMX2042Y12V
var fullResult=matchingResult.Groups[0]。值;
WriteLine($“完整结果:{fullResult}”);
//SN:(我使用这个名称:\s?“机箱”[\s\s]*?,SN:/s?(.{11})
正则表达式来查找所有匹配项,然后从每个匹配项中检索组1
我还假设每个序列号id正好有11个字符,因为我们需要知道序列号何时停止编辑:您可以使用(\w+)
而不是(.{11})
给你:
string someHtml = "show inventory<br><br>Name: \"My Device Name One\", DESCR: \"ASA 5506 - XSSD\"<br>PID: ASA5506-SSD , VID: N/A , SN: MSA203301BV<br>WARD-99ST-FW01#Name:\"Chassis\", DESCR: \"ASA 5506-X with FirePOWER services, 8GE, AC, DES\" < br > PID: ASA5506, VID: V06 , SN: JMX2042Y12V<br> Name: \"Chassis\", DESCR: \"ASA5506-X SSD \" < br > PID: ASA5506 - SSD , VID: N / A , SN: MSA203301BV<br> WARD-99ST - FW01#";
Regex FindHwndWrapper = new Regex("Name:\\s?\"Chassis\"[\\s\\S]*?, SN:\\s?(.{11})");
MatchCollection matches = FindHwndWrapper.Matches(someHtml);
foreach(Match match in matches)
{
Console.WriteLine(match.Groups[1]);
}
string someHtml=“show inventory
名称:\“我的设备名称一”,描述:\“ASA 5506-XSSD\”
PID:ASA5506-SSD,视频:N/A,序列号:MSA203301BV
WARD-99ST-FW01;名称:\“机箱”,描述:\“ASA 5506-X带火力服务,8GE,AC,DES\”
PID:ASA5506,视频:V06,序列号:JMX2042Y12V
名称:\“机箱”,描述:“ASA5506-X SSD\”
PID:ASA5506-SSD,视频:不适用,序列号:MSA203301BV
WARD-99ST-FW01;
Regex FindHwndWrapper=new Regex(“名称:\\s?\“机箱\”[\\s\\s]*?,序号:\\s?(.{11})”;
MatchCollection matches=FindHwndWrapper.matches(someHtml);
foreach(匹配中的匹配)
{
Console.WriteLine(match.Groups[1]);
}
正则表达式(SN:\w+)
会不会起作用?我对任何正则表达式/LinQ等都持开放态度。你有什么例子吗…我只需要检查SN中的名称:“机箱”非常感谢它工作得很好,我做了一些修改,以确保匹配精确的单词:int indexStart=body.IndexOf(“机箱”);更改为:int indexStart=Regex.Match(body,@“\WChassis\W”).Index;
string someHtml = "show inventory<br><br>Name: \"My Device Name One\", DESCR: \"ASA 5506 - XSSD\"<br>PID: ASA5506-SSD , VID: N/A , SN: MSA203301BV<br>WARD-99ST-FW01#Name:\"Chassis\", DESCR: \"ASA 5506-X with FirePOWER services, 8GE, AC, DES\" < br > PID: ASA5506, VID: V06 , SN: JMX2042Y12V<br> Name: \"Chassis\", DESCR: \"ASA5506-X SSD \" < br > PID: ASA5506 - SSD , VID: N / A , SN: MSA203301BV<br> WARD-99ST - FW01#";
Regex FindHwndWrapper = new Regex("Name:\\s?\"Chassis\"[\\s\\S]*?, SN:\\s?(.{11})");
MatchCollection matches = FindHwndWrapper.Matches(someHtml);
foreach(Match match in matches)
{
Console.WriteLine(match.Groups[1]);
}