Home > Enterprise >  How to match strings between two words but display also the first word
How to match strings between two words but display also the first word

Time:08-16

I want to match specific string between two words and display also the first word. The string between two words is “First Time : 10:10PM”.

I have regex matching between two words but it displays all between >Z and First Time.

https://regex101.com/r/iVBUCQ/1

Data:

qitjfjdjqkfjjf 1934848[*.    {*}*}*#*#*#[#*
]*,qgvv]*?£[£?£,£~'_!~£[££<£<'<'?  £]!<!<
['~£,'}'<',!']'',',    <€~Z1234566789>Z12345667890
1'fncnr'qmtjcsmsj194&($.!:!
,$/&15?'?'(''(('(''158,$3,!!1
1'('(',';?1!( First Time : 10:10PM
1&4$,!;($qmfjccn1'fkfkckcqtngcnnq
AAABBB : ,$2$$(&158((&&,&,&;&(&&((&

Desired Result:

>Z12345667890 First Time : 10:10PM

CodePudding user response:

You may use this regex with 2 capture groups:

\b[AZQ]\d{10,14}(>\S ).*?(First Time : \d\d:\d\d[AP]M)

RegEx Demo

  • \b[AZQ]\d{10,14}: Match word boundary followed by letter [AZQ] followed by 10 to 14 digits
  • (>\S ): Capture group #1 to match > followed by 1 non-whitespace chars
  • .*?: Match any text or line break
  • (First Time : \d\d:\d\d[AP]M): Capture group #2 to match First Time : followed by hour:minute and AM or PM
  • .*?: Match any text or line break
  • \s: Match a whitespace
  • AAABBB\b: Match AAABBB and word boundary
  • Related