简体   繁体   中英

How can I extract a dynamic length string from multiline string?

I am using "nslookup" to get machine name from IP.

nslookup 1.2.3.4

Output is multiline and machine name's length dynamic chars. How can I extract "DynamicLengthString" from all output. All suggestions IndexOf and Split , but when I try to do like that, I was not a good solution for me. Any advice ?

Server:  volvo.toyota.opel.tata
Address:  5.6.7.8

Name:    DynamicLengthString.toyota.opel.tata
Address:  1.2.3.4

I made it the goold old c# way without regex.

string input = @"Server:  volvo.toyota.opel.tata
Address:  5.6.7.8

Name:    DynamicLengtdfdfhString.toyota.opel.tata
Address:  1.2.3.4";

string targetLineStart = "Name:";
string[] allLines = input.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

string targetLine = String.Empty;
foreach (string line in allLines)
    if (line.StartsWith(targetLineStart))
    {
        targetLine = line;
    }

System.Console.WriteLine(targetLine);

string dynamicLengthString = targetLine.Remove(0, targetLineStart.Length).Split('.')[0].Trim();


System.Console.WriteLine("<<" + dynamicLengthString + ">>");
System.Console.ReadKey();

This extracts "DynamicLengtdfdfhString" from the given input, no matter where the Name-Line is and no matter what comes afterwards. This is the console version to test & verify it.

You can use Regex

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {

        string Content = "Server:  volvo.toyota.opel.tata \rAddress:  5.6.7.8 \rName:    DynamicLengthString.toyota.opel.tata  \rAddress:  1.2.3.4";
        string Pattern = "(?<=DynamicLengthString)(?s)(.*$)";
        //string Pattern = @"/^Dy*$/";
        MatchCollection matchList = Regex.Matches(Content, Pattern);
         Console.WriteLine("Running");

        foreach(Match match in matchList)
        {
             Console.WriteLine(match.Value);
        }
    }
}

I'm going to assume your output is exactly like you put it.

string output = ExactlyAsInTheQuestion();

var fourthLine = output.Split(Environment.NewLine)[3];

var nameValue = fourthLine.Substring(9); //skips over "Name:    "

var firstPartBeforePeriod = nameValue.Split('.')[0];

//firstPartBeforePeriod should equal "DynamicLengthString"

Note that this is a barebones example:

  • Either check all array indexes before you access them, or be prepared to catch IndexOutOfRangeException s.
  • I've assumed that the four spaces between "Name:" and "DynamicLengthString" are four spaces. If they are a tab character, you'll need to adjust the Substring(9) method to Substring(6) .
  • If "DynamicLengthString" is supposed to also have periods in its value, then my answer does not apply. You'll need to use a regex in that case.

Note: I'm aware that you dismissed Split :

All suggestions IndexOf and Split, but when I try to do like that, I was not a good solution for me.

But based on only this description, it's impossible to know if the issue was in getting Split to work, or it actually being unusable for your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM