Regex pattern to extract text in-between 2 lines (which contains fixed keywords)

Hi Team,

Input Text -

Q012 - Decision_making: Decision making Single coded

Not back

When it comes to pain management, who makes the decision about which treatment you will take?

Normal

Q026 - Pain_state_recency: Pain state recency Multi coded

Not back | Min = 1 | Dummy

Autocode based on P3M_Pain_States and Recency_of_condition (most recent).

Normal

Q029 - Neck_back: Neck and back pain Multi coded

Not back | Min = 1 | Dummy

Autocode based on Pain_states_dummy and Body_location.

Normal


location1

... Text will be of Dynamic pattern. Need regex pattern that will help me to fetch the text between 2 lines which contains the keywords - 'Not back' and 'Normal'.
1 Like

Hi @DewanjeeS …so you want all the lines between Not Back and Normal right ?

1 Like

Hi @prasath17 …yes, there are multiple occurrences of those keywords (Not back and Normal). Need each of those texts which lies between these 2 keywords.

@DewanjeeS - Is this the output you are looking for?

Note: If you want to remove the spaces from the start and end…please use .trim option. If you want to remove the spaces in the middle of the line then you have to apply regex.replace to the output…

Hope this helps…

2 Likes

@prasath17 … Thanks for the solution suggested. we are almost there.
Last part is working fine.
For the first part, instead of selecting the text after keyword ‘Not back,’ is there a way we can get the text starting from the next line which contains the keyword ‘Not back’?

Hi @DewanjeeS …I am not following you…Could you please highlight what you are looking for as a screenshot or using Highligher so that i will take a look?

Hi @prasath17 … I am attaching screenshot of the output we are currently getting with the regex provided.
As you can see, sometimes there are certain extra texts after the keyword ‘Not back’ (like : | Min =1 | Dummy) in that same line.

So, I am only looking for output like below pattern -

1st output -
When it comes to pain management, who makes the decision about which treatment you will take?

2nd output -
Autocode based on P3M_Pain_States and Recency_of_condition (most recent).

3rd output -
Autocode based on Pain_states_dummy and Body_location.

@DewanjeeS – Please try the below code in .NET Regex Tester - Regex Storm which is more reliable for UiPath and it works 100%, because https://regex101.com/ will throw an pattern error.

3 Likes

@prasath17 … 1st output coming fine (since there are no extra texts in the same line which contains the keyword ‘Not back’).
But if you see the 2nd & 3rd output, there are some extra texts coming as well (like : | Min = 1 | Dummy). I need to eliminate those as well.

@DewanjeeS - Please see my above post and the screenshot…

1 Like

@prasath17 … My bad… I did not observed the updated regex. It is working fine now… Thanks a ton for your help on this one!!

hi @DewanjeeS …If you face any issues with the above solution, you can try the below one …

In the For Each loop, you print the variables as below…

1 Like

@prasath17 … yes, i am able to retrieve this way as well… Thanks a ton.
Another query here - Is there a way we can do ‘OR’ operation with the ‘End string identifier’.
Sometimes the end identifier string can be the keyword ‘End’ as well, instead of ‘Normal’. So is there a way we can do something which will fetch the text between ‘Not back’ and ‘Normal’/‘End’?

‘|’ worked… no need to check anything further here :slight_smile:

1 Like

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