简体   繁体   中英

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

I am trying to match any character symbols in any order able to

I have three lines all combine in one line

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/)";

so far my code is

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);

I am able to match the first line which is DE-120 and extract the values from the group which I need but not able to do the second and third line.

how can I match all the characters symbols between the two braces of CHR(.....) as these values can be of any order and in any pattern,tried few patterns like.*? those didn't work

Thanks in advance

Regex: (DE\S*)[^[]*\[([^\]]*)\][^(]*\(([^)]*)\)

The 3 values you want are in groups 1, 2, and 3.

Explanation:

(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

See regex101.com for demo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM