Remove all before the last occurrence of a - (dash) regex

Asked

Viewed 90 times

-3

This is so simple but I can’t find the answer! I would like to delete everything before the last occurrence of the "-" symbol in a sentence:

I’m using a Pentaho step, Replace in String:

Regex Expression Used: -*[^-]+\d

Sentence without Regex: Rua Fulano Full, 725 - Bloco1 - 'CEP' - CIDADE Imaginaria/UF

Sentence with Regex: - Bloco1 - CIDADE Imaginaria/UF

The result I wanted was just the 'city/Uf' I tried to remove them - and then remove from the white spaces, but no longer know what to do. Thanks for your help!

  • Is there any requirement to use regex? There are simpler ways to solve it. In java/I would use the method split class String

  • 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

1 answer

2


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
  1. 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";
    
  2. 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";
    

Browser other questions tagged

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