Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex set pattern by considering parameter as text

Tags:

c#

regex

I have an a string that user may set and I would like to use that string inside pattern.

The pattern shall look like this "." + inputStr + "."

Though what if uses has regex specific charaters inside input. Pattern will break wouldnt it?

How can I create a pattern so that regex ignores what is inside inputStr?

Any ideas?

like image 856
dev hedgehog Avatar asked Jun 26 '26 02:06

dev hedgehog


1 Answers

Try:

Regex.Escape(inputStr);

This should escape the string of any characters that would muck with your RegEx (and instead interpret those characters literally).

like image 147
Kevin DiTraglia Avatar answered Jun 28 '26 16:06

Kevin DiTraglia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!