Regex to extrac text between (multi-lines)

Wanted to extract the following dynamic text on a fix template example below, can anyone help me with the ff. regex ?. Thank you.

   Regex to extract firstname which is between  "Your first name and middle initial"____Last name
    Output : Jaul A

    Regex to extract text or string between "Last name" _____"your social security number"
    Output : Staffer

    Regex to extract social security number after the text "Your social secuity number"
    Output : 153-10-6799

    Regex to extract text between "If you have a P.O box, see instructions."_____"Apt. no."
    Output : 167 Cats Black Road

    Regex to extract text after "below (see instructions)
    Output : Iphratta PA 57612

#Data

g1040Department of the Treasury—Internal Revenue Service (99)
U.S. Individual Income Tax Return
2015
OMB No. 1445-0074
IRS Use Only—Do not write or staple in this space.
Filing Status
Check only
one box.
0 Single g Married filing jointly 0 Married filing separately (MFS) D Head of household (HOH) Qualifying widow(er) (QVV)
If you checked the MFS box, enter the name of spouse. If you checked the HOH or OW box, enter the child’s name if the qualifying person is
a child but not your dependent. 210-
Your first name and middle initial Jaul A Last name Staffer Your social security number 153-10-6799
If joint return, spouse’s first name and middle initial Judeth B Last name Staffer Spouse’s social security number 185-80-9244
Home address (number and street). If you have a P.O. box, see instructions. 167 Cats Back Road Apt. no. Presidential Election Campaign Check here if you, or your spouse if filing jointly, want $3 to go to this fund. Checking a box below will not change your
City, town or post office, state, and ZIP code. If you have a foreign address, also complete spaces below (see instructions). Iphratta PA 57612
tax or refund. II You 0 Spouse
Foreign country name Foreign province/state/county Foreign postal code If more than four dependents,
see instructions and / here 1. II
Standard
Deduction
Age/Blindness

Hi,

Hope the following helps you.

Regex to extract firstname which is between “Your first name and middle initial”____Last name
Output : Jaul A

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Your first name and middle initial).*?(?=Last name)").Value

Regex to extract text or string between “Last name” _____“your social security number”
Output : Staffer

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Last name).*?(?=Your social security number)").Value

Regex to extract social security number after the text “Your social secuity number”
Output : 153-10-6799

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Your social security number )[-\d]*").Value

Regex to extract text between “If you have a P.O box, see instructions.”_____“Apt. no.”
Output : 167 Cats Black Road

System.Text.RegularExpressions.Regex.Match(yourString,"(?<= If you have a P.O. box, see instructions\.).*?(?=Apt\. no\.)").Value

Regex to extract text after "below (see instructions)
Output : Iphratta PA 57612

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=below \(see instructions\)\.).*").Value

Main.xaml (7.7 KB)

Regards,

1 Like

(?<=Your first name and middle initial).(?=Last name)
(?<=Last name).
(?=Your social security number)
(?<=Your social security number\s)([0-9]{3}-[0-9]{2}-[0-9]{4})
(?<=If you have a P.O box, see instructions.).(?=Apt. no.)
(?<=below (see instructions)).

Please use the above one

1 Like

Thank you @Divyanshu_Divyanshu and @Yoichi, Appreciated.

1 Like

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