How to check whether the value contains the special character or not?

How to check whether the value contains the special character or not ?
If yes, how to filter that special character from the string?
Can anyone please help

1 Like

Hi @divyaag you can use regex activities in uipath like ismatch activity with expression, which would return boolean value as whether it has or not

"^[!@#%^&*(),.?":{}|<> ]*"

And this xaml can help you, it has sample of your issue been resolved @divyaag
regex.xaml (4.9 KB)

You can also remove or replace the special characters in a string buddy with match activity, but with the above expression you can find the value containes special character or not

3 Likes

Is that working buddy.! @divyaag

@Palaniyappan , unfortunately no …

Its returning true even if there are no special characters. Can you please help

1 Like

"^[A-Za-z0-9 ]+$"

Oh no worries @divyaag use this to check whether it has other characters apart from abcd or 1234
that would give false if it has any special characters or gives us true if the string has any one special characters … @divyaag

This worked kindly refer this xaml
regex.xaml (4.8 KB)

Thank you @Palaniyappan. yes it worked for me :slight_smile:

Can you please help me to remove the special characters from the string?

Sure @divyaag
System.Text.RegularExpressions.Regex.Replace(YOUR VARIABLE HERE, β€œ^[A-Za-z0-9 ]+$”, β€œβ€)

Sorry @Palaniyappan, its not working again

1 Like

@Palaniyappan was almost right

you need System.Text.RegularExpressions.Regex.Replace(youvariable, β€œ[^A-Za-z0-9 ]”, β€œβ€)
And carefull with the quotes, if you just copy-paste from here, it won’t work, you need to manually replace them.

Fine @divyaag
Its resolved

here is the xaml for your reference
regex.xaml (5.0 KB)

Cheers

1 Like

is that working now…i checked and sent you a xaml, it worked @divyaag

Best regex pattern you could use in this scenario would be [^\w].

[^\w]
Match a single character not present in the list below [^\w]
\w matches any word character (equal to [a-zA-Z0-9_])

Please note _ is not considered as a special character when you use [^\w].

If you want to allow a special character, you can add it to the pattern.
So if you want to allow @ just change the pattern to [^\w@]

inputString = "I @am #a $clean{ string ;now."
pattern1 = "[^\w]"
pattern2 = "[^\w .]"
pattern1Output = System.Text.RegularExpressions.Regex.Replace(inputString, pattern1, "")
pattern2Output = System.Text.RegularExpressions.Regex.Replace(inputString, pattern2, "")

In the above example
pattern1 allows only a-z, A-Z, 0-9 and _
pattern2 allows a-z, A-Z, 0-9, _, space and period.

Outputs for the above examples would be.
With pattern 1: Iamacleanstringnow
With pattern 2: I am a clean string now.

Sample Visual Basic code: https://rextester.com/OZSH19294

Regular expression is a powerful tool and I would recommend everyone to learn it. I used regex101.com to learn it.

3 Likes

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