How to make working two different regex in one?

Viewed 52

I search to match date for a specific key, but currently I success to either match all date, or match my specific key but not with date separate.

Here is a more visual explication

in Input I have

Mystring ="
Text("List1:[2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, 2020-09-03 02:30, 2020-09-03 04:30, 2020-09-03 07:25, 2020-09-03 10:00, 2020-09-03 10:02],
Text("List2:[2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, 2020-09-03 02:30, 2020-09-03 04:30, 2020-09-03 07:25, 2020-09-03 10:00, 2020-09-03 10:02]
"

with this function, I match all date of Mystring

 RegExp regExp79 = new RegExp(r'\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}');
 mylistdate = regExp79.allMatches(Mystring).map((z) => z.group(0));
 print(mylistdate);

But output take all date ( list1 and List2) I try to match only list1

So I try to make character limitation. For example if I want only date from list1, I tried to match Text("List1:[. and ]

All these regex work separatly but don't found how to make work together

Text\("list1:\[    => start limitation
    
(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}) => extraction of date 

(.*?)] => end of limitation 

but if I melt all it's not work, it select only first date, I would match all date of the limited selection

Text\("List1:\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}).*?]    

https://regex101.com/r/6adb1u/1

1 Answers

You can use

(?<=Text\("List1:\[[^\][]*?)\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(?=[^\][]*])

See the regex demo

Details

  • (?<=Text\("List1:\[[^\][]*?) - a positive lookbehind that requires Text("List1:[ and then any 0 or more chars other than square brackets (as few as possible) immediately to the left of the current location
  • \d{4}-\d{2}-\d{2}\s\d{2}:\d{2} - 4 digits, -, 2 digits, -, 2 digits, a whitespace, 2 digits, : and 2 digits
  • (?=[^\][]*]) - a positive lookahead that requires any 0 or more chars other than square brackets (as many as possible) followed with a ] char immediately to the right of the current location.

Code example:

String Mystring ="\nText(\"List1:[2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, 2020-09-03 02:30, 2020-09-03 04:30, 2020-09-03 07:25, 2020-09-03 10:00, 2020-09-03 10:02],\nText(\"List2:[2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, 2020-09-03 02:30, 2020-09-03 04:30, 2020-09-03 07:25, 2020-09-03 10:00, 2020-09-03 10:02]";
RegExp regExp79 = new RegExp(r'(?<=Text\("List1:\[[^\][]*?)\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}(?=[^\][]*])');
print(regExp79.allMatches(Mystring).map((z) => z.group(0)).toList());

Output:

[2000-00-00 00:00, 2020-09-02 04:30, 2020-09-03 00:30, 2020-09-03 02:30, 2020-09-03 04:30, 2020-09-03 07:25, 2020-09-03 10:00, 2020-09-03 10:02]
Related