Regex to extract if key exists multiple times

Since there are 2 DATA keyword , I only want to extract the data after the last DATA

Data
$31,993.92
51.1.018
$31,993.92
Data
$11111

Out : $11111

Current implementation : (?<=DATA\s)[\d,.$]+

Hi,

Can you try RightToLeft option as the following?

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Data\s+)[\d,.$]+",System.Text.RegularExpressions.RegexOptions.RightToLeft)

Or we can also get it using Matches method and Last method for result collection.

Regards,

1 Like

Hi @AhmedKutraphali

As @Yoichi said u can try with matches too

You can also try matches activitiy for this

  list_1= System.Text.RegularExpression.Regex.Matches(input,"(?<=Data[\s\r]+).*")

Now use list_1.ElementAt(list_1.Count-1). ToString

To retrive the last element

1 Like

elementlist is not a member of system.text.regularexp.atch collection

Hi,

Can you try the following?

System.Text.RegularExpressions.Regex.Matches(input,"(?<=Data[\s\r]+).*").Cast(Of Match).Last.Value

Or Match class doesn’t have ElementAt. Can you try Item instead of it?

Regards,

what is the datatype of the variable ?

what is the variable type for this one ?

is this IEMatch or match collection ?

String right.

Thanks for the help and idea guys . appreaciated.

Hi,

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Data\s+)[\d,.$]+",System.Text.RegularExpressions.RegexOptions.RightToLeft)

This returns Match type. So to be precise, I should write as the following.

System.Text.RegularExpressions.Regex.Match(yourString,"(?<=Data\s+)[\d,.$]+",System.Text.RegularExpressions.RegexOptions.RightToLeft).Value

This returns String type.

System.Text.RegularExpressions.Regex.Matches(input,"(?<=Data[\s\r]+).*").Cast(Of Match).Last.Value

The above also returns String type. If there is possibility no match, get matchCollection type then check it.

mc = System.Text.RegularExpressions.Regex.Matches(input,"(?<=Data[\s\r]+).*")
if mc.Count =0 .....

Regards,

1 Like

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