Get part of a string with Regular Expression - C #

2

Based on the string "Plane Liberty Company +50 - 043-98965-2784 (058 / POS / SMP)", I need to get the part of "043-98965-2784".

I noticed that in the txt file I'm using, the numbers follow the pattern "000-00000-0000".

Could anyone help me?

    
asked by anonymous 31.10.2017 / 15:40

1 answer

2

Would that be

var match = Regex.Match(str, @"(\d{3}-\d{5}-\d{4})");
The \d captures any digit, the number in braces tells the number of digits to be captured and the stroke is a literal, ie, it captures a stroke.

Complete code:

using static System.Console;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var str = "Plano Liberty Empresa +50 - 043-98965-2784(058/PÓS/SMP)";            
        Match match = Regex.Match(str, @"(\d{3}-\d{5}-\d{4})");
        Write(match.Groups[1].Value);
    }
}

See working in .NET Fiddle.

    
31.10.2017 / 15:51