You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java正则表达式处理逗号分隔字符串:提取每个块的第三个[]值

Solution for Extracting Third Bracket Values in JasperReport Expression

Since you need a one-liner for JasperReport's text field expression (which runs on Java under the hood), you can use a targeted regex with replaceAll() to directly transform your input string into the desired output.

The One-Liner Code

$F{yourInputField}.replaceAll("\\[[^\\]]+\\]\\.\\[[^\\]]+\\]\\.\\[([^\\]]+)\\]", "$1")

How It Works

Let’s break down the regex to make it clear:

  • \\[[^\\]]+\\]: Matches a single bracket pair like [xxx] — the [^\\]]+ ensures we capture everything inside the brackets except the closing ].
  • \\.: Escapes the literal dot between bracket groups (dots are special characters in regex, so we need to escape them).
  • We repeat the first two parts (\\[[^\\]]+\\]\\.) twice to skip the first two bracket pairs in each comma-separated block.
  • \\[([^\\]]+)\\]: Matches the third bracket pair and captures its content into group $1.

When you run replaceAll(), every full block like [A].[B].[C] gets replaced with just C, while the comma and space separators between blocks stay intact. This gives you exactly the output you want: Mk Alprazolam, Mk Amantadina.

Example Usage

If your input string is stored in a Jasper field named rawString, your text field expression would be:

$F{rawString}.replaceAll("\\[[^\\]]+\\]\\.\\[[^\\]]+\\]\\.\\[([^\\]]+)\\]", "$1")

内容的提问来源于stack exchange,提问作者Jose Antonio Hernandez Gil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:01:00