2

Hello have have this string

  <20170503-18:19:09, FIXT.1.1:BANZAI->EXEC, event> (Initiated logon request)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, incoming> (8=FIX.4.4☺9=67☺35=A☺
  34=177☺49=EXEC☺52=20170503-18:19:09.298☺56=BANZAI☺98=0☺108=30☺10=092☺)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, event> 
  (Received logon) fdsfhffghgfhgjgf  177☺49=EXEC☺52=20170503-18:19:09.298☺
  56=BANZAI☺98 (Received logon) more stuff after this....

I want to find if after keyword "Initiated logon request" there is a following response "Received logon" when I write my regex pattern like this to accommodate for multi-line and any character in between

 Initiated logon request.*[\S\s]*Received logon 

I get everything up the the last keyword in the string result

  Initiated logon request)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, incoming> (8=FIX.4.4☺9=67☺35=A☺
  34=177☺49=EXEC☺52=20170503-18:19:09.298☺56=BANZAI☺98=0☺108=30☺10=092☺)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, event> 
  (Received logon) fdsfhffghgfhgjgf  177☺49=EXEC☺52=20170503-18:19:09.298☺
  56=BANZAI☺98 (Received logon

what I want to do is to stop the regex when first keyword is found,

  Initiated logon request)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, incoming> (8=FIX.4.4☺9=67☺35=A☺
  34=177☺49=EXEC☺52=20170503-18:19:09.298☺56=BANZAI☺98=0☺108=30☺10=092☺)
  <20170503-18:19:09, FIX.4.4:BANZAI->EXEC, event> 
  (Received logon

Is there a way to do this thank you for the help.

Tomasz Wida
  • 329
  • 1
  • 6
  • 24

2 Answers2

7

Use lookahead positive assertion (?=...) and non-greedy quantifier *?:

Initiated logon request[\S\s]*?(?=\(Received logon\))

https://regex101.com/r/TSfjiS/3

RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

To get only upto first match of 'Received logon', use this:

Initiated logon request((?!Received logon)[\s\S])*Received logon

Regex 101

Output:

Initiated logon request)
<20170503-18:19:09, FIX.4.4:BANZAI->EXEC, incoming> (8=FIX.4.4☺9=67☺35=A☺
34=177☺49=EXEC☺52=20170503-18:19:09.298☺56=BANZAI☺98=0☺108=30☺10=092☺)
<20170503-18:19:09, FIX.4.4:BANZAI->EXEC, event> 
(Received logon
degant
  • 4,861
  • 1
  • 17
  • 29
  • Read ["When Not to Use this Technique" section here](http://www.rexegg.com/regex-quantifiers.html#tempered_greed) to understand that your suggested solution is not good to use here. – Wiktor Stribiżew May 03 '17 at 20:45
  • Wow, that makes a lot of sense. No point using an expensive solution when a lazy quantifier is more than enough! Thank you @WiktorStribiżew :) – degant May 03 '17 at 20:59