Re: Getting part of string matched with regular expressions

From: Date: Mon, 28 Jan 2008 09:17:13 +0000
Subject: Re: Getting part of string matched with regular expressions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-268189@lists.php.net to get a copy of this message
Teck schreef:
Hi, I'm trying to find a way to get part of a string where the part is matched with regular expressions. So, for example, if I have a string: a2b3cd5ef6ghi7232jklmn I need to grab "12b3cd5" using regular expressions and store the part in a variable.
what are the rules for determining "12b3cd5" is what you want? do you want to match the exact string? do you want to match the length? the combination of letters and numbers? $match = array(); $res = preg_match("#^(\d{2}[a-z]\d[a-z]{2}\d).*#", "12b3cd5ef6ghi7232jklmn", $match); if ($res) var_dump($match); else echo "no match."; the above code matches the beginning of a string that starts with 2 digits, followed by a lower case letter followed by a digit followed by 2 lower case letters followed by a digit followed by anything.
$var = do_something("","","a2b3cd5ef6ghi7232jklmn"); I was using preg_replace for this, and try to delete (i.e., replace the non-matched part with an empty string) the second part, but I can't make it work.


« previous php.general (#268189) next »