簡體   English   中英

如何從動態字符串 C# 中獲取特定字符串

[英]How to get specific string from dynamic string C#

我在提取特定字符串時遇到問題。 我有以下字符串(這可能會有所不同):

show inventory<br><br>Name: "My Device Name One", DESCR: "ASA 5506-X
SSD"<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: "Storage Device 1", DESCR: "ASA5506-X SSD"<br>
PID: ASA5506-SSD       , VID: N/A     , SN:MSA203301BV<br>WARD-99ST-FW01#

我想從這個字符串中解析“SN”,其中“Name”= Chassis。 所以從上面的字符串我需要結果=“JMX2042Y12V”。 我不想使用</br>標簽進行解析,因為它們在某些情況下不會出現。

到目前為止,我已經使用了這個:(無法正常工作,它基於我不想使用的</br>標簽。

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

我這樣打電話:

var result= ExtractFromBody(St, ", SN: ", "<br/>");

foreach (var r in result)
{
    Console.Write(r);
}

Console.ReadLine();

您可能可以對 SN 使用正則表達式(結合您現有的代碼,找到 substring 的起始索引進行搜索)

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var body = "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: \"Storage Device 1\", DESCR: \"ASA5506-X SSD\"<br>PID: ASA5506-SSD       , VID: N/A     , SN:MSA203301BV<br>WARD-99ST-FW01#";

        var regex = new Regex("(SN: )(\\w+)");

        var indexStart = body.IndexOf("Chassis");

        var substringToParse = body.Substring(indexStart);

        var matchingResult = regex.Match(substringToParse);

        //SN: JMX2042Y12V
        var fullResult = matchingResult.Groups[0].Value;
        Console.WriteLine($"Full result: {fullResult}");

        //SN: (<- contains trailing space)
        var snGroup = matchingResult.Groups[1].Value;
        Console.WriteLine($"SN group result (with trailing space): {snGroup}");

        //JMX2042Y12V
        var valueGroup = matchingResult.Groups[2].Value;
        Console.WriteLine($"Value group result: {valueGroup}");
    }
}

我使用這個Name:\s?"Chassis"[\s\S]*?, SN:/s?(.{11})正則表達式來查找所有匹配項,然后從每個匹配項中檢索組 1

我還假設每個 SN id 正好有 11 個字符,因為我們需要知道 SN 何時停止編輯:您可以使用(\w+)而不是(.{11})

這里是 go:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM