Regex for addresses (Streets, Avenues and Etc)

1

I need a regex that takes the cases that have number in the name of the address and also normal address. Number always ends after address. Example: R March 25, 200.
I need the address that would be: R March 25
E 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;
}
    
asked by anonymous 06.04.2018 / 17:26

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

It 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;
}

See Ideone

    
06.04.2018 / 18:26