This is a discussion on Getting part of string matched with regular expressions within the PHP General forums, part of the PHP Programming Forums category; Hi, I'm trying to find a way to get part of a string where the part is matched with ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. $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. |
|
|||
|
On Mon, 2008-01-28 at 14:28 +0900, Teck wrote: > 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. Rather use preg_match_all() as that will produce an array of values, with the first value being the original that you can then compare with. --Paul All Email originating from UWC is covered by disclaimer http://www.uwc.ac.za/portal/public/p...disclaimer.htm |
|
|||
|
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. > |
|
|||
|
Teck wrote:
> 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. > > $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. You can use backreferences in regular expressions. $var= preg_replace('/([a-z]+)/is','$1','azxcvjf937^88'); if($var==='azxcvjf') echo "MATCH"; http://php.net/manual/en/function.preg-replace.php look at Example1. |
|
|||
|
Teck wrote:
> 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. Assuming you meant to grab '2b3cd5': $matches=preg_match( '/2b3cd5/',$text,$match ); $var=$match[0]; /Per Jessen, Zürich |
|
|||
|
Teck wrote:
> 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. how do you decide what the above quoted string is going to be? 1. Will is always be in the same position in the string? 2. Will the pattern always be the same? (##x#xx#) ? 3. Will you be supplying the patter or search string from an outside source? > > $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. > -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare |