开发者

Coverting a basic search string to a regex

Say I have a search string like:

"Hello [NAME], how are you today? I am fine."

If I were to use a regex pattern to search text I would have to convert it to something like (assuming that '\ ' is a valid regex search for a single space):

"\Hello\ \[NAME\],\ how\ are\ you\ today\?\ I\ am\ fine."

Now before I go off and try to write a function to do this myself 开发者_运维百科is anyone aware of something that already does this sort of conversion? (Eclipse does something a bit like this; it converts all its searches into regular expressions before searching, even if you're not setting the search pattern to be a regex).

I'm targetting C# in this instance but feel free to add for other languages as other people might be interested in a similar thing for Java, Python et al.


Regex.Escape(string) will return a Regex pattern that matches the supplied literal string.

Specifically, it escapes \*+?|{[()^$.# and white space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜