An approach without regex and without using.
In my example I am using only java code. No "step" of Pentaho is used.
String endereco = "Rua Fulano Full, 725 - Bloco1 - 'CEP' - CIDADE Imaginaria/UF";
String[] arrEndereco = endereco.split("-");
String cidadeUF = arrEndereco[arrEndereco.length-1];
System.out.println(cidadeUF.trim());
//CIDADE Imaginaria/UF
In this section we have the String that we want to "extract" the value: "city/UF", for this I transform the String address into a variable of type array through the method split
, "breaking" this string by the character - (hyphenate).
In possession of the address array, according to its requirement, we want the last position of the array (which is the information of City and UF [Federativa unit]), for this we use the method length
primitive-type Array
.
Now only need to clean the data, removing the blank spaces that are present at the beginning of the string due to the way we separated the String, for this we use the method trim()
string class.
The content of the variable cidadeUF
, before executing Trim() is:
" CIDADE Imaginaria/UF"
And after executing Trim() remove the whitespace before and after.
"CIDADE Imaginaria/UF"
Another solution using the method substring
This solution aims to solve the same problem, only here we are "cropping" the value we want from the original string.
Follows the code:
String endereco = "Rua Fulano Full, 725 - Bloco1 - 'CEP' - CIDADE Imaginaria/UF";
int pontoRecorte = endereco.lastIndexOf("-");
String cidadeUF = endereco.substring(pontoRecorte+1);
System.out.println(cidadeUF.trim());
//CIDADE Imaginaria/UF
We find the last "hyphen" index in the address string (using the method lastIndexOf
), which shall be the beginning of the cut-off point.
// ↓ ponto de recorte
"Rua Fulano Full, 725 - Bloco1 - 'CEP' - CIDADE Imaginaria/UF";
Then just create a new string from the cutoff point to the end of the string. That’s what the function substring
is making.
// ↓ ponto de recorte + 1
"Rua Fulano Full, 725 - Bloco1 - 'CEP' - CIDADE Imaginaria/UF";
Is there any requirement to use regex? There are simpler ways to solve it. In java/I would use the method
split
classString
– Danizavtz
There is, the requirement is to cut the address, but I do not mean just one, but a list with but 500 record, and the best way to process this data is using the Pentaho tool, but it supports the java language, I will perform the tests with the split
– Hakkinen Diniz