1. (.*?)
2. (*)
3. #regex#
4. /regex/
A. What do the above symbols mean?
B. What is the different between # and /?
I have the cheat-sheet, but didn’t full get it yet. What i know * gets
all characters, so what .*? is for!
The above patterns are used in PHP preg_match and preg_replace.
.matches any character (roughly).*?is a so-called quantifier, matching the previous token at least zero times (and only as often as needed to complete a match – it’s lazy, hence the?).(...)create a capturing group you can refer to in either the regex or the match. They also are used for limiting the reach of the|alternation to only parts of the regex (just like parentheses in math make precedence clear)./.../and#...#are delimiters for the entire regex, in PHP at least. Technically they’re not part of the regex syntax. What delimiter you use is up to you (but I think you can’t use\), and mostly changes what characters you need to escape in the regex. So/is a bad choice when you’re matching URIs that might contain a lot of slashes. Compare the following two varaints for finding end-of-line comments in C++ style:The latter is easier to read as you don’t have to escape slashes within the regex itself.
#or@are commonly used as delimiter because they stands out and aren’t that frequent in text, but you can use whatever you like.Technically you don’t need this delimiter at all. This is probably mostly a remnant of PHP’s Perl heritage (in Perl regexes are delimited, but are not contained in a string). Other languages that use strings (because they have no native regex literals), such as Java, C# or PowerShell do well without the delimiter. In PHP you can add options after the closing delimiter, such as
/a/iwhich matchesaorA(case-insensitively), but the regex(?i)adoes exactly the same and doesn’t need delimiters.And next time you take the time to read through Regular-Expressions.info, it’s an awesome reference on regex basics and advcanced topics, explaining many things very well and thoroughly. And please also take a look at the PHP documentation in this regard.