简体   繁体   中英

Regex set pattern by considering parameter as text

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?

Try:

Regex.Escape(inputStr);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM