Regex ignore after help

Hi. I am working with a long string and extracting certain pieces of information from it, where it is in a static place within the string. I now have an issue where I have some text that can change length.

I need to extract just numbers and the decimal place which I am doing by retrieving the first 25 characters of this section of the string by using the Regex statement of System.Text.RegularExpressions.Regex.Replace(strVolume,"[^/./0-9]","")

My issue is, there can be an additional number within the substring, which gets added to end of the number and makes it incorrect. For example:

464.27856000000000Class 4 gives me 464.278560000000004
431.42171877975682, Row 1 gives me 431.421718779756821

Is there anyway I can alter my regex to ignore “Class” or “, Row” and everything after them?

Hi @fbxiii,

A possible solution could be to use Matches activity with the following pattern: “^[\d|.]+”

Best Regards,
Susana

2 Likes

Hello @fbxiii

You can try this:
result = “464.27856000000000Class 4”

result = System.Text.RegularExpressions.Regex.Replace(result.ToString.Trim.ToLower, “[a-z, ]”,"").ToString

result = System.Text.RegularExpressions.Regex.Match(result.Trim, “^[\d|.]+”).Groups(0).ToString

See attached
REGEX.xaml (7.5 KB)

2 Likes

Hi.

Thanks for the responses. I have tried that solution but it is still taking the number after the word Class.

I’ve had a bit of a read up on Regex and have found that this works to remove any digits after the words Class or Row:

System.Text.RegularExpressions.Regex.Replace(strVolume.ToString.Trim.ToLower, “[a-z,Class|Row ]\d+”,"").ToString

@fbxiii If I’ve understood your requirement correctly, this should work:

System.Text.RegularExpressions.Regex.Replace(strInputString,"[A-Za-z ,-]+","")

Let me know if this solves your problem.

Edit: This is just to remove the unwanted characters from your input. Don’t forget to use the “matches” method or the matches activity to get the apt result.

Hi Siddarth.

Thanks, but that still pulls the number from after the text at the end. I’ve found my tweak is getting the desired results.

@fbxiii apologies, but I wish the post had been a little less ambiguous for people to get the exact requirement.

I see that you’re going with your tweak. However, just to see if it’s clear this time, from this input 464.27856000000000Class 4, you want only 464.27856000000000, right?

If that’s the case, you can give this a shot once: (\d+(.\d+)?)(?=[A-Za-z,-])

Capture

This doesn’t even require you to use replace method here. You can directly use this regex in your matches and it will give you the required value “464.27856000000000” from an input like 464.27856000000000Class 4

If this still doesn’t work for you, you can always go with your alternative.

Regards,
Siddharth

1 Like

That doesn’t seem to pull anything back at all. I used:

System.Text.RegularExpressions.Regex.Match(numberThird," (\d+(.\d+)?)(?=[A-Za-z ,-])").ToString

@fbxiii Have a look at this:

RegexMatch.zip (9.5 KB)

This works for any string that is in the same format as the input cases you’ve mentioned above.

2 Likes

Yes, that does it and i’ve tried with a couple of variations. Thanks :slight_smile: