Getting part of string matched with regular expressions

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-28-2008
Teck
 
Posts: n/a
Default Getting part of string matched with regular expressions

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.
Reply With Quote
  #2 (permalink)  
Old 01-28-2008
Paul Scott
 
Posts: n/a
Default Re: [PHP] Getting part of string matched with regular expressions


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

Reply With Quote
  #3 (permalink)  
Old 01-28-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Getting part of string matched with regular expressions

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.
>

Reply With Quote
  #4 (permalink)  
Old 01-28-2008
Max Antonov
 
Posts: n/a
Default Re: Getting part of string matched with regular expressions

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.
Reply With Quote
  #5 (permalink)  
Old 01-28-2008
Per Jessen
 
Posts: n/a
Default Re: [PHP] Getting part of string matched with regular expressions

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
Reply With Quote
  #6 (permalink)  
Old 01-28-2008
Jim Lucas
 
Posts: n/a
Default Re: [PHP] Getting part of string matched with regular expressions

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:13 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0