Regex take more than one occurrence in a string

Asked

Viewed 734 times

4

I have my regex (\d{2}).

And I got my string 12 hoje vai 45 na serra pelada 55 ou 75.

How do I get my regex to pick up all occurrences of the string? It’s only picking up the last one.

1 answer

3


(\d{2})* for zero or more occurrences. (\d{2})+ for one or more occurrences.

See a test here.

To iterate over all occurrences, use:

foreach (Match m in Regex.Matches("12 hoje vai 45 na serra pelada 55 ou 75", "(\d{2})*")) 
{
    // m.Value mostra o valor encontrado, m.Index o índice na lista de 
    // expressões encontradas.
}

The reference is here.

  • 1

    What matters is to iterate Matches. Add + or * not required.

Browser other questions tagged

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