开发者

check to see if a string contains an ip address

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\Fa开发者_开发技巧lse

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.


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(match.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).


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜