Regular Expression, picking numbers between two predefined texts

Asked

Viewed 156 times

4

I need to withdraw números of a string. To string follows a pattern:

http://www.meudominio.com/1789519-texto

The number will always be between / and -

I’ve already reached the following formula:

/\d+(?=-)

The problem is that the / comes along, and I didn’t want this, I know I can cut her easily from string, but I would very much like to know how I can remove it from the result using the expression.

  • In what language?

  • c#, added the tag.

1 answer

5


Regular expression is correct. Only the desired result needs to be grouped and selected:

    var re = new Regex(@"/(\d+)(?=-)");
    var match = re.Match("http://www.meudominio.com/1789519-texto");
    Console.WriteLine(match.Groups[1]);

match.Groups[0] is the whole expression found. The next indexes are the groups, individually separated.

I made you a Fiddle.

Browser other questions tagged

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