Find index of special character using Regex

Is there a way to find the indexOf any special character if it appear within a string using linq and regex “\W”?

If the indexOf length is < 2 i’d like to remove the special character, else, keep it.

Can this be done?

Here’s an example of how you can achieve this in UiPath:

  1. Use the Assign activity to define your input string and the special character you want to find the index of:

Assign inputString = “Hello! World!”
Assign specialCharacter = “!”

  1. Use the Matches activity to find all matches of the special character using the regular expression pattern “\W”. Set the Input property to inputString and the Pattern property to "\W". This activity will give you a collection of Match objects.
  2. Use the Assign activity to find the index of the first match. Set the following properties:
  • To: index
  • Value: matches.FirstOrDefault().IndexHere, matches refers to the collection of Match objects obtained from the Matches activity.
  1. Use an If activity to check the length of the index and perform the desired action. Set the Condition property to index < 2.
  2. Inside the Then block of the If activity, use the Assign activity to remove the special character from the input string:

Assign inputString = inputString.Remove(index, 1)

  1. This will remove the special character if the index is less than 2.
  2. Finally, you can output the modified input string using a Write Line activity or perform any other required actions.

Note: Make sure to import the System.Linq and System.Text.RegularExpressions namespaces in the Imports panel of UiPath Studio to use LINQ and regular expressions.

By following these steps, you can find the index of a special character using LINQ and regex in UiPath and remove it from the string if the index is less than 2.

1 Like

we can use

or working with regex and using the index property from match

Thanks @ppr is there a way that any special character can be detected? Anything that isn’t A-ZA-z0-9

yes we can do. So with the \W you started will, So now use the index property for the evaluation and then replace if it is needed.

When furthe assistance is needed, just share some text samples with us

Thanks @ppr I’m struggling a bit with the syntax. At the moment i have:
strTest = “Bill-Bo”

The result of intTest is zero though, so it doesn’t seem to be finding the hyphen.

I’ve tried to play around with calling System.Text.RegularExpression.Regex but not sure now to combine with indexOf

No, IndexOf is not ready for RegexPattern


1 Like

Ah of course! Thank you for solving

Why use Regex when IndexOf works just fine and is simpler?

Simply because I don’t want to specify every single special character. Much easier to use Regex to see if any special character exists in the string then decide what to do with it depending on its position

1 Like

@ppr Is there a way to exclude whitespaces for this?

So Bill-bo would get picked up, but Bill bo wouldn’t?

I’ve tried:
System.Text.RegularExpressions.Regex.Match(“Bill bo”, "\W ").index
System.Text.RegularExpressions.Regex.Match(“Bill bo”, “\W\s”).index
System.Text.RegularExpressions.Regex.Match(“Bill bo”, "A-Z ").index

The above all either ignore the white space, but pick up the special character, or vice-versa. Is there a Regex that would work for this?


regex101 helps us with explanations


give a try at
[^A-Za-z0-9_ ] kindly note the space after the _ char

1 Like

Thank you very much for your continued support

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