Condition for special characters in if..!

How about the condition in if statement to check whether the expressions can have only " , " as a special character in it…?

You will have to define what is in this collection of special characters, or conversely that of allowed characters, before you can decide which cases you want to reject. Suppose your string may only contain space-like characters, letters, digits, underscores and commas. Then you can use as a condition: Regex.IsMatch(input, "^[\w\s,]*$") to check that every single character in the input string is in this set. Conversely, the expression Not Regex.IsMatch(input, "[^\w\s,]") could be used to verify that there is Not any character outside (^) the set in the given input.

3 Likes

Let me give some more examples. Remember that in regular expressions, you use [...]/[^...] to match one character in / not in the given set of characters and/or ranges. I used \w in my example, which is shorthand for a-zA-Z0-9_, alphanumeric characters and the underscore. Similarly \s matches tabs, spaces, newlines and carriage returns: \t \n\r. You can include any character literally within [] other than ^-]\, which are part of the [] syntax and must be escaped with a backslash.

So, for example:

  • ^[a-zA-Z\s,'/\\]*$: Match if, from start (^) to end ($) of string, there are zero or more (*) letters, commas, whitespace characters, single quotes, forward slashes or backslashes. In other words, either the string is empty or all of its characters are in this list.
  • [^a-zA-Z\d\s,]: Match as soon as a single character is found that is not a letter, digit, whitespace or comma. In this case you are looking for the non-matches.

There is far more to regular expressions than I could or would want to explain in a single forum post. However, I can assure you that knowing how to build and use them is an extremely useful skill, for RPA development but many other areas of development as well.

So, for some further reading/practicing:

  • More on character classes;
  • Here for a quick introduction to regular expressions;
  • Regex tester, correctly interprets syntax available in UiPath (.NET). Testing your patterns on real input is very important;
  • The regex activities Matches, Is Match, Replace in UiPath, available from the “Workflow Manager” package.
4 Likes