|
In Regular Expression usually a pattern should be within the delimiters "/../" or even "|..|" can also be
used.
The "\" (backslash) meta-character is used to specify that the next character
as either a special character, a literal, a back reference, or an octal escape.
All these "^.[$()|*+?{\" special characters has to be escaped or treat as ordinary character
using the backslash "\" symbol.
Few Possible Matches:
$string = "T^he $[(value)] o|f + \can be a"
Using the preg_match command in PHP following are the results.
'/\^(.*)/' Result: ^he $[(value)] o|f + can be a
'/\$(.*)/' Result: $[(value)] o|f + can be a
'/\+(.*)/' Result: + can be a
'/\((.*)/' Result: (value o|f + can be a
'/\\(.*)/' Result: \can be a
PHP Example:
<?php
$string = 'T^he $[(value o|f + can be a' ;
preg_match('/\^(.*)/', $string, $matches);
echo $matches[0];
print "<br>";
preg_match('/^(.*)/',$string, $matches);
echo $matches[0];
?>
Result:
^he $[(value o|f + can be a
T^he $[(value o|f + can be a
In the above regex example we have matched the "^", with and without the "backslash", when used
without the "\" the string is printed from the beginning as it behave's as a string, but when used without
a backslash its print's a string from the beginning, as it behaves like a meta-character.
Perl Example:
#! C:\programfiles\perl\bin\perl
print "content-type: text/html\n\n";
if ("This is a (templateVar)" =~ m/[\(\)]/)
print "match!\n";
else
print "no match!\n";
Result:
match!
In the second example brackets "(" is escaped or made known as the next character using the backslash
meta-character, since the string has brackets it matches.
|