Find Name with Regular Expressions

This is a discussion on Find Name with Regular Expressions within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello I'm going to expose you my problem about regular expressions. I have a list of first names and ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-02-2003
Eric VIALLE
 
Posts: n/a
Default Find Name with Regular Expressions

Hello

I'm going to expose you my problem about regular expressions.
I have a list of first names and last names at each line of a text:

$content="Bill Clinton
George W Bush
Jacques Chirac
John Fitzgerald Kennedy
Danny De Vito";

I would like to find the last name of John.

I made this function, but it works only if the last name is one word

if ( eregi("John+([a-zA-Z-])+\n", $content, $reg ) )
{
$identite = $reg[0];
print ">Identity: $identite<BR>";
}

I would like to have the text between John and the next \n

Regards, and thanks for your help,

Eric Vialle




Reply With Quote
  #2 (permalink)  
Old 07-05-2003
Rupe
 
Posts: n/a
Default Re: Find Name with Regular Expressions


"Eric VIALLE" <eric@club-competence.net> wrote in message
news:bdu8ah$5ud$1@news-reader1.wanadoo.fr...
> Hello
>
> I'm going to expose you my problem about regular expressions.
> I have a list of first names and last names at each line of a text:
>
> $content="Bill Clinton
> George W Bush
> Jacques Chirac
> John Fitzgerald Kennedy
> Danny De Vito";
>
> I would like to find the last name of John.
>
> I made this function, but it works only if the last name is one word
>
> if ( eregi("John+([a-zA-Z-])+\n", $content, $reg ) )
> {
> $identite = $reg[0];
> print ">Identity: $identite<BR>";
> }
>
> I would like to have the text between John and the next \n
>
> Regards, and thanks for your help,
>
> Eric Vialle
>
>
>
>


Try this pattern: "John([a-zA-Z ])+\n"
Note the space inside the square brackets (after 'Z').
or more generally: "[a-zA-Z] ([a-zA-Z ])+\n".
Use \- in the last class [ ] to account for hyphenated last-names.
NB: I haven't tested this - use at your own risk!

Ru


Reply With Quote
Reply


Thread Tools
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

vB 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:22 AM.


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