Match specific words after matching complete sentences using regular expressions
P粉872101673
P粉872101673 2024-04-01 14:38:05
0
2
449

I am a novice. I'm trying to find the full name in any of the lines below and there is no Obituary for

<h2>Obituary for John Doe</h2>
<h1>James Michael Lee</h1>

My regular expression is like this.

(<h1>(.+?)<\/h1>|<h2>Obituary\sfor\s(.+?)<\/h2>)

What I got was still John Doe's obituary. How do I delete the obituary for ?

P粉872101673
P粉872101673

reply all(2)
P粉775788723

Can you do something like this without using regular expressions?

/**
 * @description : Function extracts names from html header tags
 * @example : "

Obituary for John Doe

James Michael Lee

" -> ["John Doe", "James Michael Lee"] * @param $html string * @return []string : list of full names */ function extractFullNames($html) { $regex = '/(.*?)/'; preg_match_all($regex, $html, $matches); $names = $matches[1]; $names = array_map('trim', $names); $names = array_map('strip_tags', $names); $names = array_map('strtolower', $names); $names = array_map('ucwords', $names); $names = array_map('removeObituary', $names); return $names; } /** * @description : Function used to remove "Obituary For" if present * @example : "Obituary For John Doe" -> "John Doe" * @param $name string * @return string : name without "Obituary For" */ function removeObituary($name) { $name = str_replace("Obituary For ", "", $name); return $name; } // Test cases $html = '

Obituary for John Doe

James Michael Lee

'; $names = extractFullNames($html); $expected = ['John Doe', 'James Michael Lee']; echo "Expected: " . implode(', ', $expected) . "\n"; echo "Actual: " . implode(', ', $names);
P粉394812277

All roads lead to Rome, you may do this:

|2>Obituary\sfor\s)\K[^>

Please view this demo at regex101. The match will be in $out[0].

\K Reset Start reporting the match. For more information, see SO Regular Expressions FAQ.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!