How to get data between two String

Hi,

How do i get string between two string(“for”).
For ex.:- “Outstanding Bill for xyz company for Mar 20”
expected output: xyz

But sometime company name contain
So basically i want complete string between first “for” and last “for”.

Please Suggest

Hi @TUSHAR_DIWASE

Use Assign activity
LHS - Create an variable
RHS - System.Text.RegularExpressions.Regex.match(InputString, “(?<=Outstanding\sBill\sfor\s)(\S+)”).Tostring

Regards
Gokul

Hi @TUSHAR_DIWASE ,

Is the keyword company always present?
If so, then this ought to work:

image

System.Text.RegularExpressions.Regex.Match(str_variable,"[^\s]+(?=\scompany)").Value

Kind Regards,
Ashwin A.K

@ashwin.ashok thanks but “Outstanding Bill” and both “for” word are constant.
so i want string between first and last “for”

HI @TUSHAR_DIWASE

Try this expression

Use Assign activity
LHS - Create an variable
RHS - System.Text.RegularExpressions.Regex.match(InputString,“(?s)for(.*?) for”).Groups(1)

Regards
Gokul

1 Like

@Gokul001 an you please assist me about the LHS type of variable

Hi @TUSHAR_DIWASE ,

Is this what you meant?

image

image

System.Text.RegularExpressions.Regex.Match(str_variable,"(?<=for\s)(.*)(?:\s.*)(?=\sfor)").Groups(1)

Kind Regards
Ashwin A.K

1 Like

Hi @TUSHAR_DIWASE

String Type variable


getting below error

@Gokul001 string is not accepting variable type

HI @TUSHAR_DIWASE

Try this expression

System.Text.RegularExpressions.Regex.Match("for is for","(?s)for(.*?) for").Groups(1).ToString

Regards
Gokul

End of the expression after Groups(1) just give the .Tostring

Regards
Gokul

1 Like

You can use the below regex for getting the data previous to fixed variable

**

[^\s]+(?=\scompany)

**

Use this regex syntax to get the data between two labels/fixed variables

**

(?<=for).*(?=for)

**

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