Regex for addresses (Streets, Avenues and Etc)

Asked

Viewed 1,137 times

1

I need a regex that takes cases that have number in the address name and also normal address. The Number always ends after the address.
Example: R 25 March 200.
I need the address that would be: R 25 March
And address number: 200

Regex regex = new Regex(@"\D+");
match = regex.Match("R 25 de março 200");
if (match.Success) {
string endereco = match.Value;
}
  • Why -1? I would like to know why.

  • What is the default? the address will always end with the house number?

  • Yeah, it always ends with a house number.

  • Already tried to split by white space and catch the last occurrence?

  • No, M. Bertolazo, I need in regex even.

  • Put a $ at the end of your regex and see if that’s what you want: new Regex(@"\D+$");

Show 1 more comment

1 answer

2


You can use regex:

([\w\W]+)\s(\d+)

Explanation:

Grupo 1: ([\w\W]+) -> pega qualquer caractere antes do último espaço
\s -> espaço separador, não pertence a nenhum grupo
Grupo 2: (\d+) -> pega somente números após o último espaço

Would look like this:

Regex regex = new Regex(@"([\w\W]+)\s(\d+)");
var match = regex.Match("R 25 de março 200");
if (match.Success) {
    string endereco = match.Groups["1"].Value;
    string numero = match.Groups["2"].Value;
}

Check it out at Ideone

  • Thank you very much, You solved my doubt.

Browser other questions tagged

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