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.

Rinse, repeat.

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?

Edit:

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?

Answers


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.


Need Your Help

MVC Html.DisplayNameFor on a complex model

asp.net-mvc-4 razor

I have a model with a complex type i.e. User/Addresses/List of Addresses

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.