Using Regex With Different Text Format


I am trying to use regex to capture text that comes after a certain phrase. The information after the phrase can be either in a strait line or in a paragraph format. Below is an example.

Service For 12345 apple street Tampa,FL 34567


Service For
12345 apple street
Tampa,FL 34567

So, I am trying to find a regex expression that will work for both because it is unsure which kind of format will be given.

@Sahil_J_Patel - Here you go…

Added \r?\n?\r?\n? to handle both CRLF Carriage Returns and line breaks…

(?<=Service For\s+\r?\n?\r?\n?).+\r?\n?\r?n?.+

Hope this helps…

Hi @Sahil_J_Patel ,
try this:

output = System.Text.RegularExpressions.Regex.Match(input,"(?<=Service For\s).+$",System.Text.RegularExpressions.RegexOptions.Singleline).Value

Preview link: regex101: build, test, and debug regex

1 Like

Hi Prasath,

How would I change this so it would work when it is “service for” or “service address”?

@Sahil_J_Patel - Like this…

(?<=Service (For|Address)\s+\r?\n?\r?\n?).+\r?\n?\r?n?.+

Regex Pattern Link

1 Like

Thank you Prasath!

1 Like

Sorry Prasath,

one follow up question. When I type that expression in it also highlights address/for. Is there a way to not highlight address/for?

Also say for example the desired text was was not 2 lines but 3 like below what would I need to add. I am not very familiar with regex syntax.

Service For
1234 apple st

@Sahil_J_Patel - Please check this.
My only concern is if the address comes only 2 line…this code will capture the 3 line too…if you have blank that is fine, if not then we need to see…

or try using Adrian’s code , in the Regex builder you just have to choose Single Line …but i guess his code will also capture the 3rd Line…

I am thinking of it now…but until then please proceed with my new code…

Okay makes sense. My final question is on extracting a number. If the keywords are for example amount due, amount, balance due and balance and I want to extract the number after those keywords how would I do so? Examples would be like

Total amount due [random number of spaces] $25.29

Total balance due [random number of spaces] $58.12

Pay this amount [arrow symbol] $126.13

Amount due

@Sahil_J_Patel - Please see the below

(?<=(Total amount due|Total balance due|Amount due|Pay this amount).*)\r?\n?\r?\n?([\$\d.,]+)$

Note: Above one works in Regular Regex Matches I can take Group2 output and prints it. But I am not sure, how we can make it work in Regex based extractor, I hope it should work. you can play around by removing each texts and you can see the amount captured in the group2.

Hope this helps…

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