Need help in validating a String using a custom format using Regex

hey Folks!

Thanks to the help provided by people from this forum, i’ve been able to extract, and split a string according to certain parameters. However, I now need to validate this string that i’ve extracted in a custom format.

The custom format can be either:

  1. 10Chars-PTE-01 (can range from 01-09) e.g. 1234567890-PTE-01; or
  2. 9Chars-PTE-01 (can range from 01-09) e.g. 123456789-PTE-03

Is there an activity or regex that can help me in validating this format? and subsequently returning a boolean value.

All advice and help will be greatly appreciated, thanks much in advance!

Use IsMatch activity, with an regex like \d{9,10}-PTE-0\d

1 Like

Hi,

Can you try the following in If condition?

System.Text.RegularExpressions.Regex.IsMatch(strData,"^\d{9,10}-PTE-0\d$")
(In case 10Chars and 9 Chars can have only numeric )

Regards,

1 Like

We can use Is Match activity.
Regex : “\d{9,10}-PTE-\d{2}”

wow! thanks everyone for the quick replies! i’ve tested and though it works… i just realised my custom format has another variation to it e.g. T17FC0060B-PTE-01.

And although the first part of the string contains 10 characters, the regex returned “False”.

How do i incorporate even alphabets into the condition?

Replace the 1st \d with .
This way, it will check for any characters, not only digits.

try this ,

(\d|[A-Z]){9,10}-PTE-\d{2}

Thanks!

So the it should look something like this…?
like “.{9,10}-PTE-0\d“

Yes. But with that will also match T1-F!0060B-PTE-01 for example

I’ll give it a shot once i’m done with my commute. Thanks!

Would there be one that will only validate alphanumerics?

The one kadiravan_kalidoss sayd.

Okay. Will give it a try later, thanks for the responses!

@kadiravan_kalidoss and @RobertD

The code seems to work well to validate only alphanumeric characters. Though it validates well for anything below 9 characters, the result seems to return “True” even though if my string has more than 10 characters…

Am i doing something wrong here? haha

Hi,

The following might help you

System.Text.RegularExpressions.Regex.IsMatch(strData,"^\w{9,10}-PTE-0\d$")

Regards,

hey Yoichi!

Thanks once again for helping me out on this :slight_smile: this works perfectly!

Would i then be able to expand on this statement to include another acceptable format? e.g. i would like to accept this string input even if they omit the dashes like T17FC0060BPTE01

Is the statement as simple as “^\w{9,10}-PTE-0\d$|^\w{9,10}PTE0\d$” ?

Hi,

"^\w{9,10}-?PTE-0\d$" will work. Can you try?

Regards,

hey Yoichi!

i’ve edited the statement to “^\w{9,10}-?PTE-?0\d$” to take care of the 2nd hyphen as well and it works well. Thank you so much everybody!

1 Like

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