简体   繁体   中英

Php regex word lookahead

I am trying to scrape a webpage for a person's name and am having trouble factoring in the possibility that a middle name may also be present. For example, if i am trying to match the name "John Smith" on a webpage and that page has the name with a middle name included (for example John Mark Smith or John M Smith or John M. Smith or JohnM.Smith), the search will turn up empty even though technically the first and last name are mentioned together on the page.

Is there a regex that can take into account a one word hop in the middle of matching the name? The name is in the variable:

$fullname = "John Smith";

How can i accomplish this?

I would try this (or something similar):
replace the space with " ?[a-zA-Z.]* ?"
John ?[a-zA-Z\\.]* ?Smith
http://www.phpliveregex.com/p/3jh

preg_match("/(John.*?Smith|Smith.*?John)/", $input_line, $output_array);

看到这里: http : //www.phpliveregex.com/p/3jj

The question has been slightly expanded to include the possible reverse order.

Try:

(John ?[\w\.]* ?Smith|Smith ?[\w\.]* ?John)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM