0

Is there way to pass some string to regexp and not worry about ecranisation of special chars.

For example I wont to find line which starts with words "\north+west\", as you can see "\n" and "h+" should be ecranised. So question is there some special combination to write text as it is?

/^\s+(<some special combination> \north+west\)\s+/i

or maybe you know function which can properly ecranise my text?

redexp
  • 4,765
  • 7
  • 25
  • 37
  • 1
    Most languages provide some function to escape strings that should be inserted into a regular expression. Which language do you use? – Till Helge Apr 12 '13 at 13:02
  • 2
    Please note that the description for the `regex` tag states: *Please also include a tag specifying the programming language or tool you are using.* – Jon Clements Apr 12 '13 at 13:04
  • If this is JS, this is already asked several times on SO. – nhahtdh Apr 12 '13 at 13:26
  • @TillHelgeHelwig right now I need it in PHP but I thought there can be standard way for all PCRE languages – redexp Apr 12 '13 at 15:06

2 Answers2

1

In PHP and Perl you can use \Q...\E delimiters to autoescape metacharacters inside regexp. Quoting the doc:

\Q and \E can be used to ignore regexp metacharacters in the pattern. For example: \w+\Q.$.\E$ will match one or more word characters, followed by literals .$. and anchored at the end of the string.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • just found [\Q...\E doesn't work in JavaScript](http://stackoverflow.com/a/5664273/815295) "Hulk sad" :( – redexp Apr 12 '13 at 15:20
  • Yep, it doesn't work in JS; that's why you were asked to provide a language tag as well, btw. ) But there's a plenty of 'autoescaping' code out there; in fact, jQuery UI (and other popular tools) have the corresponding methods in their libraries. – raina77ow Apr 12 '13 at 15:22
0

In addition to @raina77ow answer, when you use pcre via a language like PHP that needs pattern delimiters, you can't use the \Q...\E feature if your string contains the opening or the closing delimiter. For example, you can't write patterns like:

/\Qabc/def\E/
~\Qabc~def\E~
[\Qabc[def\E]
[\Qabc]def\E]
(\Qabc)def\E)
(\Qabc(def\E)

The only way is to use the preg_quote function and to put the delimiter (only if this one isn't already a special regex character) in its second parameter.

Community
  • 1
  • 1
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125