REGEX look ahaed to another word

hi there,

i am trying to extract some data using regex

what i am trying to get is everything from test 1 or test 2 up to FAIL from the string:
test1 query test FAIL hello test2 query test 2 FAIL xyz abc test1 query test PASS
test3 query test FAIL test1 query 4 test FAIL

so the result should be
query test, query test 2, query 4 test

any help apprecaited

thanks

Hi @adrian_sullivan

How about this expression

System.Text.RegularExpressions.Regex.Match(YourString,"query\stest\s\d+|query\s\d+\stest|query\stest").Tostring

image

Regards
Gokul

very close - just need to include test1 or test2 in results

Result should be this on right?

Which test1 or test2 can you tell ? @adrian_sullivan

How about this expression? @adrian_sullivan

System.Text.RegularExpressions.Regex.Match(YourString,"query\stest\s\d+|query\s\d+\stest|query\stest(?=\sFAIL)").Tostring

image

Hello @adrian_sullivan
KIndly check this

(?<=test[\d])\D+\d+(?=\sFAIL)|(?<=test[\d])\D+(?=\sFAIL)

image

that is returning data from test 3 (which i dont nee) and missing the final test 1 data

that is returning data for test 3 which i do not need

HI @adrian_sullivan

How about this expression?

System.Text.RegularExpressions.Regex.Match(YourString,"(?<=test1\s)query\stest(?=\sFAIL)|(?<=test2\s)query\stest\s\d+(?=\sFAIL)|query\s\d+\stest").Tostring

Regards
Gokul

Hi @adrian_sullivan

Try this

Expression

test(1|2).*?(?= FAIL)

How about this

(?<=test1|test2 ).*?(?=\sFAIL)

image

that looks much better - is there anyway to include test1 and test2 in the output

Hi @adrian_sullivan

System.Text.RegularExpressions.Regex.Match(YourString,"test1\squery\stest(?=\sFAIL)|test2\squery\stest\s\d+(?=\sFAIL)|test1\squery\s\d+\stest").Tostring

image

Regards
Gokul

@adrian_sullivan Check this

(?=test1 |test2 ).*?(?=\sFAIL)

image

1 Like

thanks that is working for me. i spent two days looking at it so this is great

@adrian_sullivan
Its my pleasure

Happy Automation :smile:

Regards
Gokul Jai

@adrian_sullivan , Kindly mark the solution, it will helps for someone finding solution with same issues.

Regards,
Gokul Jai

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.