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
相关产品推荐
相关产品推荐

