Home > Back-end >  regex pattern to get date after a particular text and a combination of word-number string ending wit
regex pattern to get date after a particular text and a combination of word-number string ending wit

Time:10-23

I would like to create a regex which can match the following pattern

HR060178 RGP LUKA RIJEKA 30.09.2022 09:42:52 22HR060178U0078350MRN

to be specific the date which comes after text LUKA RIJEKA and the MRN number starting with 22 The screenshot is of another problem where in the highlighted numbers need to be extracted.

CodePudding user response:

since you only provided this one sample, its diffucult to test edge cases and fine tune the regex, but the following should give you a good starting point:

((?<=LUKA\sRIJEKA\s)\d\d\.\d\d\.\d\d\d\d)|((?<=\sLUKA\sRIJEKA\s\d\d\.\d\d\.\d\d\d\d\s\d\d:\d\d:\d\d\s)22[^\s] ?MRN)

i have split your problem into two match groups, one that looks for the date and one which looks for the MRN.

i would advice you to read through this article: Regex lookahead, lookbehind and atomic groups. since the solution heavily relies on lookaheads. for creating and testing regexes i would recommend you to use one of the many websites which come up when you search for "regex online"

  • Related