How to Extract string between two strings? but consider a certain string as end

I have a regex below that extract data between two strings , the start text is the “In the Last 120 Days” text and the end text is “History” text, but in the string or text there are many “History” text , I only wanted to get the first found “History” text as an end and the rest would be ignored , How do we do that in regex"
System.Text.RegularExpressions.Regex.Match(strInput,"(?<=In the Last 120 Days)([\S\s]*)(?= History)").Value.Trim

I’m still learning RegEx myself so I couldn’t help you with that, but here’s an Assign function you could use in UiPath with some string manipulation, to achieve what you want.
myString = StrReverse(Split(StrReverse(Split(myString, StartTextToRemove,2)(1)), StrReverse(EndTextToRemove),2)(1))

And an example of it being used:
ExtractString.xaml (5.5 KB)

But I’ll be watching this post to see how this could be done with RegEx :slight_smile:

if the sample text is this

"In the Last 120 Days Something happened in History, and other tzhings were cool in history too and In the Last 120 Days and history stuf… and this is History test 2312dsad ytrrytryrt History "

the output should be “Something happened in”

since “Something happened in” is between start “In the Last 120 Days” and end “History” which is the first History text found and the rest will be ignored.

Hi @Jelrey,
Try this regex

System.Text.RegularExpressions.Regex.Match(strInput,"(?sm)(?<=In the Last 120 Days)\w+(?= History)").Value.Trim

Mark this as a solution if this helps.
Regards,
Aditya

Hi @Jelrey,

Sorry for the above example is your text constant every time or it changes?
if yes use this
System.Text.RegularExpressions.Regex.Match(strInput,"(?sm)(?<=In the Last 120 Days)\w+(?= History,)").Value.Trim

Regards,
Aditya

does this ignored the other History text ?

Ow sorry, I thought It should read between the first appearance of “Something happened in” and the last appearance of “History”.
It can still be done quite simply with the string manipulation, and it would ignore the rest of the text, but the answer from @desineediaditya looks cleaner and is in RegEx as you wanted :slight_smile:

yes @Jelrey