I'll be extracting up to 4 parts of a String that come from user-input. All 4 parts need to be in order i.e. (1-4) below in that order
The first capturing group is required and the last few are not. The named capturing groups are described below
KEY(This can be either
CONF|ESD|TRACKINGand needs to be followed be either
[:;'\s]\*s) -- This group is required
DATAThis can be any text except for any of the 2 patterns described below (Should assume multiple whitespace can lead this following
LINE_DATAThis is a string in the following kind of format
"1(2),3(4),5(6)"and should account for spaces in between chars i.e.
" 1 ( 2 ) , 3(4 ), 5 6) "This capture can only come after a match of
"L[:';\s]\s*]"...but I don't want to capture this part. I just want to capture the
"1(2),3(4)..."part (and exclude any trailing and leading whitespace).
INITIALSThis is the last part of the string and would come before a
\s*$. It's a pattern that would be
"*sm", "*jdm", "*pL"should all match. Again...this group can be optional too and I don't want any leading/trailing whitespace.
Note: this is all case-insensitive too.
Examples with expectations
"CONF: FEDEX 12345 L: 12(2),2(9),32 *SM"
[KEY=>'CONF' , DATA => 'FEDEX 12345', LINE_DATA => '12(2),2(9),32', INITIALS=>'*SM']
[KEY: 'ESD', DATA: '12/12/92']
"tRacking' my data L: 1,2,3(4) ";
[KEY=>'tRacking', DATA=>'my data' LINE_DATA: '1,2,3(4)']
My regex is below
https://regex101.com/r/Sw8UXC/1 is an interactive playground with the regex.
What I'm looking for in review
- Are there any flaws with this regex and are there foreseeable issues where it would fail with what I need?
- Is there a way to simplify it further?
- Any other assumptions I should be making because this comes from user-input?
I'm a little unsure of the '
\Ks, but it made it so that I only got the 4 parts I needed (i.e. no whitespace in between groups was also matched on (regardless of being captured)