Regex help reqd

Input String:
"Dear abc,

This is regarding reference number 826123, the following info have been identified:

Task: 18212402 Description: 121 - gas leakage ,not completed Price: $98.50
Task: 32321720 Description: Rejected Price: $0.00
Task: 34017280 Description: 131 - This will -be repaired once spare available Price: $38.50
Task: 66017240 Description: 139 - sent for -repair Price: $16.50

Thank you,

xyz.
Email: s@a.com"

my regex :
(?<=Description:).*(?=-)

I want 121,131,139 as output,just want text after description and first ‘-’.

@prateekjain1992 Check below regex once.

 "(?<=Description:).*?(?=-)"

Hi @prateekjain1992,
This will work :wink:

(?<=:.)\d{3}.\W

Hi @prateekjain1992,
You can try to experiment with something like this:


But be careful as it’s case sensitive.

Input string-
ar abc,

This is regarding reference number 826123, the following info have been identified:

Task: 18212402 Description: 121 - gas leakage ,not completed Price: $98.50
Task: 32321720 Description: Rejected Price: $0.00
Task: 34017280 Description: 131 - This will -be repaired once spare available Price: $38.50
Task: 66017240 Description: 139 - sent for -repair Price: $16.50

Thank you,

xyz.
Email: s@a.com

I want to extraxct,whatever is after first(-) and before ‘price’, and if (-) not present then after ‘description:’ and before ‘price’.

so in above string i want— ‘gas leakage ,not completed’
Rejected
This will -be repaired once spare available
sent for -repair

I tried using or condition (?<=Description:).?(?=Price)|(?<=Description:[A-Z]).?(?=Price) ,but couldn’t get desired output.

1 Like

Hi @prateekjain1992,
I moved the topic here because it’s still about the same. There is no need to open separate topic.

@prateekjain1992 Split the text( ex variable readText) with newline and then use statement assign the output of below statement to new string variable(example str).
Here item refers to each line.

if(item.Contains("-"),System.Text.RegularExpressions.Regex.Match(item,"(?<=-).+(?=Price)").ToString,System.Text.RegularExpressions.Regex.Match(item,"(?<=Description:).+(?=Price)").ToString)

using if activity str not equal to null then print str variable value otherwise don’t do nothing.

you can also use linq query like below.

readText.Split({Environment.Newline},StringSplitOptions.RemoveEmptyEntries).Select(Function(d) if(d.Contains("-"),System.Text.RegularExpressions.Regex.Match(d,"(?<=-).+(?=Price)").ToString,System.Text.RegularExpressions.Regex.Match(d,"(?<=Description:).+(?=Price)").ToString)).ToArray().Where(Function(x) Not String.IsNullOrEmpty(x)).ToArray()

I know this is not optimal solution, but you can use for time being.

Hi @prateekjain1992

Below syntax should give our desired text.

System.Text.RegularExpressions.Regex.Matches(input_string, "(?<=\- )(.*?)(?= Price\:)")

Note: The assign variable datatype should be array or enumerated.