Regex Error

I currently am using Regex to get the numerical value between two brackets which works using the match activity. and assigns that value to a variable…however It gets an error message and stops working if a letter is accidentally inserted. is there a way to have it recognize and not catch the letter?

image

@UiUser

Hello, I’ve had a look at your regex, the issue is with the * wildcard

Try this

(?<=[)\d{0,9}(?=])

Is that only going to return values if they are of value 0-9? because there could be double digits as well as decimals…

It will match any digit, up to 9 places, if you want more just add more, if you want to capture only two digits, do 0,2

Does this make sense? :slight_smile:

After re Reading your question, i didn’t allow for decimals this should work
(?<=[)\d{0?.9}(?=])

@kieranc


after testing it the regex picks up other numbers but i just need the ones in the brackets

Hello @UiUser,

This should work fine: (?<=[)([0-9]+.[0-9]+|[0-9]+)(?=])

Thanks,
Adit

@UiUser Replace the .* in the middle of your query with \d+

Doing this means it will match anything with one or more digits between the brackets. Therefore if it is [7k] it will not pick it up. If you wanted to still get 7 as a match then let me know and i can help with another solution

Yes I still wanted it to match the 7 but to just ignore any other characters that are between the brackets
so for example:
[7.0]=7.0
[7.6k] = 7.6
[21k.0] = 21.0
[32…4] = 32.4

If this is not possible is there a way to do a try catch to catch these input errors?

Although it’s possible to do in a single regex statement, it is much easier to read if you separate into parts. Make sure you import the System.Text.RegularExpressions namespace into Studio

YourText is the InputString you are searching with Regex
MatchList is a List(of string) that will store the strings you get using regex. I use a list, but you can use any array or collection

  1. Assign MatchList = Regex.Matches(YourText,"(?<=[).*(?=])").Cast(Of Match).Select(Function(x) x.Value).ToList()
  2. For Each item in MatchList (this should be of type string)
    a. Assign item = Regex.Replace(item,"[^\d.]",String.Empty)

Now you have a list of strings that contain only numbers and decimals. NOTE: If there are multiple decimals in between the brackets it will pull all the decimals. If that is a possibility for you, then I can help rework it a bit - you would simply need to add a bit more processing in the for each loop to remove decimals after the first occurrence using an if statement