Java : Pattern How to match any character or symbol in any order between the two braces

标签 java regex

我正在尝试以任何顺序匹配任何字符符号

我有三行,全部合并为一行

line 1 = DE-120  LLL[310]  CHR(9049         9360          TRANSNSW      400000000000000018416)

line2 =  DE-121  LLL[035]  CHR(00317010024/VER/1//CRT/UCAF//POS/2/)


line3 =  DE-123  LLL[151] (/VER/1//MSO/SYSTEST_MSO_DASH//TXD/2020-04-20T03:47:47.492Z//AQU/SYSTEST_ACQ_S2I//TTY/AUTH//OID/311e12cc-4561-4b0a-9540-f30d5ef8d744//CLI/IUV5OJDNV5LZG/)";

到目前为止我的代码是

import java.util.regex.Matcher;
import java.util.regex.Pattern;


Pattern patternreq = Pattern.compile("(DE\\W*\\d*)(\\s*\\w*\\W*)(\\d*)(\\W*\\s*)(\\w*)(\\()([\\w*\\s*]+)(\\))");

String txn = "DE-120  LLL[310]  CHR(9049                                                 9252                                                    9360                                                            9408000000009556SYSTEST_MSO_DASH                        TRANSNSW        9661293208721                               400000000000000018416)DE-121  LLL[035]  CHR(00317010024/VER/1//CRT/UCAF//POS/2/)DE-123  LLL[151]  CHR(/VER/1//MSO/SYSTEST_MSO_DASH//TXD/2020-04-20T03:47:47.492Z//AQU/SYSTEST_ACQ_S2I//TTY/AUTH//OID/311e12cc-4561-4b0a-9540-f30d5ef8d744//CLI/IUV5OJDNV5LZG/)";

Matcher m = patternreq.matcher(txn);

   String field = matcher_request.group(1);
   String length = matcher_request.group(3);
   String value = matcher_request.group(7);

我能够匹配第一行 DE-120 并从我需要的组中提取值 但无法执行第二行和第三行。

如何匹配 CHR(.....) 的两个大括号之间的所有字符符号,因为这些值可以是任何顺序和任何模式,尝试了一些模式,如 .*?那些不起作用

提前致谢

最佳答案

正则表达式:(DE\S*)[^[]*\[([^\]]*)\][^(]*\(([^)]*)\)

您想要的 3 个值位于组 1、2 和 3 中。

说明:

(DE\S*)     Capture text starting with `DE` and up to whitespace
[^[]*\[     Skip up to and including `[`
([^\]]*)    Capture everything up to and excluding `]`
\]          Skip `]`, must be present
[^(]*\(     Skip up to and including `(`
([^)]*)     Capture everything up to and excluding `)`
\)          Skip `)`, must be present

参见regex101.com用于演示。

关于Java : Pattern How to match any character or symbol in any order between the two braces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61316622/

相关文章:

java - 如何通过Java中的Reflection API加速类扫描?

java - 链接方法模式

java - 如何在 Java 中使用 Comparator 进行排序

javascript - 正则表达式需要匹配字符串中的第一个路径而不是后续

regex - Swift 覆盖 + 多种类型和长表达式

java - 在Java中实现单例模式与工厂模式相结合的最佳方式是什么?详细描述

java - 尽管实现了构造函数,但构造函数丢失错误

regex - scrapy/xpaths/正则表达式 : proper xpath/re to ignore "link interjections"

java - 如何根据标点符号和空格拆分字符串?

python查找文件中正则表达式匹配次数最多的部分