Get a match in a text with regular expressions

4

I'm doing a practice in c # and I would like to know how I can get a substring of a string that meets some requirements.

For example, I would like to take out what corresponds to a IP that would be 192.168.1.1 . I had thought about regular expressions, but I do not know how to do it, since the following code does not return any value to me.

 Regex ipV4 = new Regex(@"^\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
 string s = "naieofnai555aedae192.168.1.1andaiodane";
 Match match = ipV4.Match(s);            

 if (match.Success)
 {
    Console.WriteLine("IP was {0}", match.Groups[1].Value);
 }
    
asked by jooooooooota 28.04.2017 в 17:14
source

3 answers

7

The problem is that you are using ^ and \b in your expression and none of the 2 is fulfilled.

  • ^ matches the start of the text.
  • \b matches a full word limit (this would work if there were no letters, numbers or underscores around the IP).

This is explained in Delimiters .

Also, you are using .Groups[1] , but that is used when there are parentheses within the regular expression (this is explained in The captured group ).

If you really want to extract any group of 4 digits separated by periods, you only need to delete those delimiters.

Get only the first match

using System.Text.RegularExpressions;
string ipv4 = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
string s   = @"naieofnai555aedae192.168.1.1andaiodane";

Match m = Regex.Match(s, ipv4);
if (m.Success)
{
    Console.WriteLine("IP encontrado: {0}", m.Value);
}

Demo: link


Find all matches

using System.Text.RegularExpressions;
string ipv4 = @"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}";
string s    = @"naieofnai555aedae192.168.1.1andaiodane111.222.111.222ooooo";

foreach (Match m in Regex.Matches(s, ipv4)) //bucle para obtener todas las coincidencias
{
    Console.WriteLine("IP encontrado: {0}", m.Value);
}

Demo: link

Alternatives to match an IP

  • You can write more limited (although it would not change the efficiency):

    string ipv4 = @"\d{1,3}(?:\.\d{1,3}){3}";
    
  • Or you could only allow numbers between 0 and 255 (to extract any IP).

    string ipv4 = @"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}";
    
  • answered by 28.04.2017 / 17:30
    source
    2

    Try this validation:

    Regex ip = new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}");
    string input = "naieofnai555aedae192.168.1.1andaiodane";
    MatchCollection result = ip.Matches(input);
    Console.WriteLine(result[0]);
    

    Testing the answer of federhico with the first case if it returned well, but the second does not, because the ip is not separated by spaces in the text "input". That is, it works when "naieofnai555aedae 192.168.1.1 andaiodane". Explanation given by Mariano ...

        
    answered by 28.04.2017 в 17:28
    -1

    Based on this Question , I'd tell you to try with

    ((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]?)
    

    or

    Regex ip = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
    MatchCollection result = ip.Matches(input);
    Console.WriteLine(result[0]); 
    
        
    answered by 28.04.2017 в 17:22