Condition for if activity in datatable

datatable

#1

Dear reader,

Today I developed a datatable which contains postal codes in the format: “1234AB”. (four numbers and two letters, without space)

I would like to check with an if activity for each row if the data in this column complies with the format “1234AB”.

Does anyone know which condition for this if activity fits best with my question?

Best, Matt


#2

@mattsheep

Try using the Is Match activity with the regex pattern below:

^[0-9]{4}[A-z]{2}$

That should tell you if the value in the column of your datatable meets the format you described. If you wanted only uppercase change the “z” to a “Z” and uncheck the RegexOption property of “IgnoreCase”.


#3

Hi. May I recommend Regex.

Finding the Regex pattern that meets your needs can be tricky.
If you can assume it’s always 4 digits followed by 2 letters, then you can use a pattern like this:
pattern = "^[0-9]{3,4}[a-zA-Z]{2}$"
which says starts with (notated by the ^) 3 to 4 numbers and 2 letters to end the string (notated by the $)

forgive me if I am mistaken on the pattern, because I didn’t verify this online.

so with that pattern you can use this in your If condition:

System.Text.RegularExpressions.Regex.Match(rowitem, pattern).Success

another tip is that you can test your pattern in a message box with a string to make sure it is working correctly. You can also use .Value or other methods instead of .Success. I used Success because it returns a boolean.

Hope this helps.

Regards.


#4

Dear @Tyler_Williams and @ClaytonM,

This is perfect, thank you both so much. Your solution is amazing!!

Best, Matt