Regex to find specific sentence

Hello,

image

I need a REGEX to find only 4th sentence which is: “I need this sentence to be catched by regex”
Only criteria is that the 2nd sentence is fixed, it never changes so we need to use it in regex, other sentences can change.

For example, I can find the 3rd sentence by using this:
(?<=Hello My Name is Jennifer[\n\r]+).*(?=[\n\r]+)

I need a similar solution based on the 2nd sentence to catch 4th one this time.

Thanks in advance.

Hello @Jenni_K

Have a look at this solution.
Main.xaml (4.8 KB)

Regex Pattern:
(?<=My name is [A-Za-z-]+\n.*\n).*

This pattern will work on any name (even with dashes) - like Jennifer, Angus and Marie-Lou.

Hopefully this helps :blush:

image

3 Likes

Hello Steven,

Could you please read it from a text file instead of using string. I tried that and got an object reference error with your regex solution.

Hi,

I recommend using \r?\n for linebreak. This supports both linebreak of Windows (CRLF) and Unix (LF).

Can you try the following pattern?

(?<=Hello My Name is Jennifer\r?\n.*?\r?\n).*(?=\r?\n)

Regards,

4 Likes

Hi @Jenni_K,

regex101: build, test, and debug regex

(?<=My Name is ).*

Regards,
Arivu

@arivu96…just fyi…she wanted to capture the 4th sentence …pls chk your regex

(?<=My Name is )(\w+)

Regards,
Arivu

This one worked perfectly. Thanks a lot :partying_face:

2 Likes

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