正则表达式文字-文本跨度 [英] Regular expression literal-text span
问题描述
有什么方法可以向正则表达式指明要显式搜索的文本块吗?我问是因为我必须匹配一段非常长的文本,其中包含各种元字符(并且(并且必须完全匹配),然后是一些灵活的东西(足以值得使用正则表达式),然后是更多的文本必须完全匹配.
Is there any way to indicate to a regular expression a block of text that is to be searched for explicitly? I ask because I have to match a very very long piece of text which contains all sorts of metacharacters (and (and has to match exactly), followed by some flexible stuff (enough to merit the use of a regex), followed by more text that has to be matched exactly.
冲洗,重复.
不用说,我真的不想遍历整个事情并且不得不转义每个元字符.这只是让它读起来很不舒服.有没有办法包装这些部分,这样我就不必这样做了?
Needless to say, I don't really want to have to run through the entire thing and have to escape every metacharacter. That just makes it a bear to read. Is there a way to wrap those portions so that I don't have to do this?
具体来说,我正在使用 Tcl,并且通过元字符",我的意思是有各种长字符串,例如**$^{*$%\)
".我真的不想逃避这些.我的意思是,它会在字符串中添加数千个字符.Tcl regexp 是否有文字文本跨度元字符?
Specifically, I am using Tcl, and by "metacharacters", I mean that there's all sorts of long strings like "**$^{*$%\)
". I would really not like to escape these. I mean, it would add thousands of characters to the string. Does Tcl regexp have a literal-text span metacharacter?
推荐答案
在 Tcl 中执行此操作的正常方法是使用帮助程序进行转义,如下所示:
The normal way of doing this in Tcl is to use a helper procedure to do the escaping, like this:
proc re_escape str {
# Every non-word char gets a backslash put in front
regsub -all {\W} $str {\\&}
}
set awkwardString "**$^{*$%\\)"
regexp "simpleWord *[re_escape $awkwardString] *simpleWord" $largeString
如果你有一个完整的文字字符串,你还有另外两个选择:
Where you have a whole literal string, you have two other alternatives:
regexp "***=$literal" $someString
regexp "(?q)$literal" $someString
然而,这两种模式都只允许纯文字模式;你不能那样混合模式和文字.
However, both of these only permit patterns that are pure literals; you can't mix patterns and literals that way.
这篇关于正则表达式文字-文本跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!