List Info

Thread: Re: extract all numbers from a long string




Re: extract all numbers from a long string
user name
2007-11-04 19:55:50


On Oct 31, 8:31 am, Jeff <jeff.hill...gmail.com> wrote:
> Pattern: "^(d+)w*s*(.*)$"
> Replacement: "$2#$1"
>
> This assumes you are operating on each string, one at a
time.  The
> first set of parentheses captures the number at the
beginning of the
> string.  The "th", "st",
"nd", etc. and any white space is then
> matched, but not captured.  The second set of
parentheses captures
> everything else.  The replacement string is just the
second capture, a
> "#", and then the first capture.
>
> I hope this helps.
>
> Jeff
>



Hi Jeff,
Thanks a million for the help - I really appreciate it. It's
very
close to working now - I just need to tweak the pattern to
catch all
the numbers in the string regardless of where they are
positioned. Is
that possible?
for example:

31st Sutton Coldfield (3rd Streetly)
returns
Sutton Coldfield (Streetly)#31#3

Thanks again!
Ciarán


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: extract all numbers from a long string
user name
2007-11-04 22:36:45
On Nov 5, 8:55 am, Ciaran <cronok...hotmail.com> wrote:
> On Oct 31, 8:31 am, Jeff <jeff.hill...gmail.com> wrote:
>
> > Pattern: "^(d+)w*s*(.*)$"
> > Replacement: "$2#$1"
>
> > This assumes you are operating on each string, one
at a time.  The
> > first set of parentheses captures the number at
the beginning of the
> > string.  The "th", "st",
"nd", etc. and any white space is then
> > matched, but not captured.  The second set of
parentheses captures
> > everything else.  The replacement string is just
the second capture, a
> > "#", and then the first capture.
>
> > I hope this helps.
>
> > Jeff
>
> Hi Jeff,
> Thanks a million for the help - I really appreciate it.
It's very
> close to working now - I just need to tweak the pattern
to catch all
> the numbers in the string regardless of where they are
positioned. Is
> that possible?
> for example:
>
> 31st Sutton Coldfield (3rd Streetly)
> returns
> Sutton Coldfield (Streetly)#31#3
>
> Thanks again!
> Ciarán

I'm sorry I missed the fact that position isn't important. 
One way to
do this is to use the count parameter of the preg_replace
function:

$count = 0;

do
{

   $string = preg_replace(
"^(.*?)(d+)[a-z]+s+(.*?)$",
                           "$1$3#$2",
                           $string, -1, $count );
}
while ( $count > 0 );

Good luck.

Jeff


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )