0

I have this String:

String a = [MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Alprazolam], [MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Amantadina]

I need to extract just the 3rd [value] of every comma separated block. For example, I need to print: Mk Alprazolam, Mk Amantadina

I'll put it in a textfield expression of a JasperReport so I need to achieve it in one line. Example:

a.substring(0, a.indexOf(",")).substring(a.substring(0, a.indexOf(",")).lastIndexOf("[")).replace("[", "").replaceAll("]", "")

this returns: Mk Alprazolam... but I need it for every comma separated block (it could be 1+).

I've tried some regular expressions with .replaceAll but can't achieve it.

Any help would be appreciated.

Thanks

  • Look in to string splitting: https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java You could split that string 2 times and get what you want – Tadija Bagarić May 10 '18 at 17:29

4 Answers4

0

Assuming the input string is always in the format you have mentioned (else you need to handle some corner cases), you can simply do

Arrays.stream(a.split(","))
            .map(str -> str.substring(str.lastIndexOf(".") + 2, str.length() - 1))
            .forEach(System.out::println);
  1. We split the input string by ,.

  2. For each part (from above) extract the substring starting at last index of '.' + 2 and ending at index one before the end of the string

  3. Print each of them.

You can join the individual strings as

String result = Arrays.stream(a.split(","))
            .map(str -> str.substring(str.lastIndexOf(".") + 2, str.length() - 1))
            .collect(Collectors.joining(" , "));
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
0

Pure replaceAll() solution:

s = s.replaceAll("\\[[^\\]]*\\]\\.|[\\[\\]]", "");

See regex101 for demo.

Explanation

Initial text:

[MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Alprazolam], [MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Amantadina]

First it matches [xxx]. and replaces them with nothing. That leaves:

[Mk Alprazolam], [Mk Amantadina]

It then also matches [ and ] and replaces them with nothing. Result:

Mk Alprazolam, Mk Amantadina

Andreas
  • 154,647
  • 11
  • 152
  • 247
0

Here's how to extract your desired info with a regex:

String a = "[MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Alprazolam], [MercadosArmados.GpoMerc-CTN3].[Mercado Marketing SNC].[Mk Amantadina]";
Pattern p = Pattern.compile("(?:\\[[^\\[]*\\]\\.){2}\\[([^\\[]*)\\]");
Matcher m = p.matcher(a);
while (m.find()) {
    System.out.println(m.group(1));
}

Will print:

Mk Alprazolam
Mk Amantadina

You can also test a regular expression online.

rustyx
  • 80,671
  • 25
  • 200
  • 267
0

You could use the following regex and take the 3rd (or the ones where the modulo of 3 equals 0) match from it

(?:((?<=\[)(.*?)(?=\])))+

This regex used lookaheads and lookbehind to see if there is a bracket before or after the targeted string.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89