Showing posts with label regular expressions. Show all posts
Showing posts with label regular expressions. Show all posts

Thursday, September 26, 2013

What do lazy and greedy regular expressions mean?

Greedy expression tries to get as much as possible.
If you write a regular expression like this
<.+>
for the string:
<p>My expression</p>
you will get all the string
<p>My expression</p>
Oh, yes.. this thing is really greedy.

Let's take a look at the lazy one. If you write a regular expression like
<.+?>
We have just added "?". That means we want the smallest possible group. Now we have this result:
<p>
Here you are. The laziest thing you have ever seen :)

Tuesday, January 24, 2012

Method preg_match() for Cyrillic characters in PHP

Sometimes I encounter the problem with preg_match() method, because it doesn't treat Cyrillic characters the proper way. The problem also corresponds to other non-latin characters.
There is a solution for the search in the string that contains non-latin characters.
<?php 
  preg_match("/^[a-zA-Z\p{Cyrillic}]+$/u", "AбВгд");
?>
Do not forget about the Pattern Modifier - u
u (PCRE_UTF8)

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8.

This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.