1

Hi i am having a problem with my application i want a part of string from a string.The situation is i am having a string variable named as stringData it stores following values:

IP Address         Hardware Address         Lease expiration                Type
192.168.1.2        00-23-8B-87-9A-6B        Mon Jan 02 01:14:00 2006        Dynamic
192.168.1.3        F8-F7-D3-00-03-80        Mon Jan 02 01:14:00 2006        Dynamic 
192.168.1.4        F8-F7-D3-00-9C-C4        Mon Jan 02 01:14:00 2006        Dynamic 
192.168.1.5        F0-DE-F1-33-9C-C4        Mon Jan 02 01:14:00 2006        Dynamic 

I just want Hardware Address starting with F8-F7-D3-00. I am using a substring method now but i am getting only one matched hardware address but there might be a possibility of multiple strings as in above e.g and i want all of them.I am using C#. Any help would be highly appreciable.

9
  • How you are using the substring() at the moment? Commented Apr 1, 2014 at 8:36
  • Is stringData a single line or the whole table-like text? Commented Apr 1, 2014 at 8:38
  • Robert..It is a table like text Commented Apr 1, 2014 at 8:44
  • //int index = stringData.IndexOf("F8-F7-D3-00"); // //string sub = stringData.Substring(index, 17); // //Console.WriteLine(sub); Commented Apr 1, 2014 at 8:46
  • Is the whole table stored in a single string variable? Are the columns separated by tab or spaces? Commented Apr 1, 2014 at 8:47

4 Answers 4

1

Try this one:

string pattern = @"(F8-F7-D3-00\S+)";
string input = "IP Address         Hardware Address         Lease expiration                Type\n"+
    "192.168.1.2        00-23-8B-87-9A-6B        Mon Jan 02 01:14:00 2006        Dynamic\n"+
    "192.168.1.3        F8-F7-D3-00-03-80        Mon Jan 02 01:14:00 2006        Dynamic \n"+
    "192.168.1.4        F8-F7-D3-00-9C-C4        Mon Jan 02 01:14:00 2006        Dynamic \n"+
    "192.168.1.5        F0-DE-F1-33-9C-C4        Mon Jan 02 01:14:00 2006        Dynamic";

MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
   Console.WriteLine("Hardware Address: {0}", match.Groups[1].Value);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Split the string into separate lines first like this:

        string[] linesep = new string[] {"\r\n"};
        string[] dataLines = stringData .Split(linesep, StringSplitOptions.RemoveEmptyEntries);

Then apply your Substring method to each:

foreach (line in dataLines)
{
    if (line.Contains(yourIPstring)
    // .. do your stuff
}

You may have to change the line separators.

Comments

0

You can easily use Regular expression

var file = File.ReadAllText(@"....\Input.txt");

Regex r = new Regex("F8-F7-D3-00(-[0-9A-F]{2}){2}");
var matches = r.Matches(file);
foreach (Match m in matches)
{
    Console.WriteLine(m.Value);
}

Comments

0

If the string contains tabs you could split it by '/t' (a horizontal tab).

string hardwareAddresses = stringData.split('/t').ToList().Where((item, index) => index % 4 == 1);

hardwareAddresses will then contain an array of hardware addresses

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.