Regular Expression Patterns in Apache RewriteCond and Friends: Full or Partial Match?

Apache documentation indicates that

CondPattern is a condition template, a regular expression that applies to the current instance of TestString [...] CondPattern is a perl compatible regular expression with some additions [...]

And gives examples like

RewriteCond %{REMOTE_HOST} ^host1.* [OR] 

I know about Perl regular expressions, but it seems confusing to me:

Are we talking about a partial match here ? (The pattern corresponds to some substring inside the string, as in Perl $string =~ m/pattern/; ;?)

Or is it rather a complete match ? (The pattern matches the entire string , as in Java Pattern.matches(pattern, string) ?)

If the match is partial, the final .* Seems redundant. It is complete, then it is ^ that which seems redundant.

Many of the examples I found in Apache docs and everywhere have (seeming to me) inconsistency.

Update: this is a problem with documents, now it is fixed.

+6
source share
2 answers

Partial (In the example .* Is redundant.)

Apache uses the excellent PCRE library (although the older version is Rev: 5.0 2004 the last time I checked), written by Philip Hazel, which is very similar to Perl. But I agree that many of their examples leave much to be desired regarding the accuracy of regular expressions.

There are several extensions related to Apache, for example. adding to the template ! the leader ! to apply the NOT logic to a match. The best documentation I've already found (which you probably already saw) is on this page: Apache module mod_rewrite

+6
source

Thanks. I deleted the unnecessary. * Of the regular expressions that I believed were referenced in documents. Please let me know if you find others, and I will correct them too.

Regular expressions by default correspond to a substring and do not have to correspond to the entire string (usually). Places in Apache docs that cause errors and need to be fixed.

People tend to quit. * into regular expressions all the time when they are completely unnecessary.

+5
source

Source: https://habr.com/ru/post/885453/


All Articles