tags:

views:

91

answers:

3

Hi,

I have a file loaded into a stream reader. The file contains ip addresses scattered about. Before and after each IP addresses there is "\" if this helps. Also, the first "\" on each line ALWAYS comes before the ip address, there are no other "\" before this first one.

I already know that I should use a while loop to cycle through each line, but I dont know the rest of the procedure :<

For example:

Powerd by Stormix.de\93.190.64.150\7777\False

Cupserver\85.236.100.100\8178\False

Euro Server\217.163.26.20\7778\False

in the first example i would need "93.190.64.150" in the second example i would need "85.236.100.100" in the third example i would need "217.163.26.20"

I really struggle with parsing/splicing/dicing :s

thanks in advance

* I require to keep the IP in a string a bool return is not sufficient for what i want to do.

+6  A: 

EDIT: Edited to take account of the "slash at beginning and end" part.

Try to match each line against a regex of (all as one string; split for readability).

\\(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\

Full sample:

using System;
using System.Text.RegularExpressions;

class Program
{
    private static readonly Regex Pattern = new Regex
        (@"\\(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}" +
         @"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\");


    static void Main(string[] args)
    {
        Console.WriteLine(ContainsAddress("Bad IP \\400.100.100.100\\ xyz"));
        Console.WriteLine(ContainsAddress("Good IP \\200.255.123.100\\ xyz"));
        Console.WriteLine(ContainsAddress("No IP \\but slashes\\ xyz"));
        Console.WriteLine(ContainsAddress("Long IP \\123.100.100.100.100\\ x"));
    }

    static bool ContainsAddress(string line)
    {
        return Pattern.IsMatch(line);        
    }
}
Jon Skeet
yes this also works! but it doesnt offer me any way of using the IP :< is there a way to adapt the code for this?
brux
+1  A: 
using System.Text.RegularExpressions;

…

var sourceString = "put your string here";
var match = Regex.Match(sourceString, @"\b(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b");
if(matches.Success) Console.WriteLine(match.Captures[0]);

This will match any IP address, but also 999.999.999.999. If you need more exactness, see details here: http://www.regular-expressions.info/examples.html

The site has lots of great info an regular expressions, which is a domain-specific language used within most popular programming languages for text pattern matching. Actually, I think the site was put together by the author of Mastering Regular Expressions.

update

I modified the code above to capture the IP address, as you requested (by adding parentheses around the IP address pattern). Now we check to make sure there was a match using the Success property, and then you can get the IP address using Captures[0] (because we only have one capture group, we know to use the first index, 0).

Jay
this is great and it works! but what if i want to actually take the ip and keep it in a string for later use instead of just getting a bool return?
brux
i noticed on the site you refered me to smt abut a capturing group in order to keep the matched values, but i dont understand it :(, and advice?
brux
@bruk I updated the answer to show how to capture and get the value of the IP address.
Jay
thank you very much you a smart guy
brux
A: 

Looks like, for each line, you're looking for "^.*?\\(?<address>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\\.*$"

To break this down:

^ - matches the beginning of a line, helping ensure you'll start matching at the right point.

.*? - matches any character zero or more times, but as few times as possible.

\ - matches the backslash character. Coupled with the two prior terms, this will get us to the first backslash of a line so we can capture the next term.

(?) - specifies a named group of characters that can be referred to from within matches. The text of the full match will be the entire line the way this is written, but this named group will be only what you're looking for out of the match.

[0-9]{1,3} - matches a sequence of between 1 and 3 digit characters. The [0-9] is equivalent to \d but I find that when a regex has fewer backslashes and more characters you'd normally see in the string, it's more understandable.

. - matches a period.

.* - matches any character zero or more times. Used to skip to the end.

$ - matches the end of a line.

KeithS
yes tanks keith, does the regex class off any way to keep the string which it matched? i need to keep the IP's and not just return a bool
brux
@brux You can get hold of the match group using Regex.Match(string, pattern).Groups("address")
PHeiberg