Get part of a string with Regular Expression - C#

Asked

Viewed 210 times

2

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

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

Could someone help me?

1 answer

2


Would that be

var match = Regex.Match(str, @"(\d{3}-\d{5}-\d{4})");

The \d captures any digit, the number between keys tells the number of digits that must be captured and the dash is a literal, ie captures a dash.

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 on . NET Fiddle.

  • Very good! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.