by RegexNinja » Mon May 18, 2020 7:16 am
kgng97:
Without seeing more examples, its hard to advise just one specific regex.
Here's some matches to work on the example, in order of how many names they could match.
Start with the bottom match, then work your way upwards (if you need to match more names).
If you need help making any of the groups more specific/relaxed, just post back with details.
Btw .*?_ matches the 1st-underscore, while .*_ matches the last-underscore, so you were almost there.
Cheers!
Matches:
^(.*?)(_.*?)(_.*?)(_.*)$
^(.*?)(_.*?)(_.*?[A-Z])(_.*)$
^(.*?)(_.*?)(_.*?\d[A-Z])(_.*)$
^(.+?)(_.+? .+?)(_.*?[\d][A-Z])(_.+)$
^(.+?)(_[A-Z]{2,5} .+?)(_.*?[\d][A-Z])(_.+)$
^(.+? [A-Z]{1,3})(_[A-Z]{2,5} .+?)(_\d{4}-\d{2}-[\d]{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} .+?)(_(?:19\d\d|20\d\d)-\d{2}-[\d]{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} .+?)(_(?:20[0-2]|19[7-9])\d-\d{2}-\d{2}[A-Z])(_.+)$
^(.+? [A-Z]{2})(_[A-Z]{3} [a-z]+?)(_(?:20[0-2]|19[7-9])\d-[01]\d-[0-3]\d[A-Z])(_\d_.+)$
Replace:
\1\3\2\4
Result:
Lastname AB_LCH clin_2019-07-26A_3_Blood test report_ocr ---> Lastname AB_2019-07-26A_LCH clin_3_Blood test report_ocr