简体   繁体   English

正则表达式通过将参数视为文本来设置模式

[英]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 + "." + 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? 如何创建一个模式,以便正则表达式忽略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). 这应该逃避任何会破坏您的RegEx的字符串(而不是字面上解释这些字符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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