Home  >  Q&A  >  body text

Match specific words after matching complete sentences using regular expressions

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粉872101673P粉872101673224 days ago452

reply all(2)I'll reply

  • P粉775788723

    P粉7757887232024-04-02 10:10:01

    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 = '/(.*?)<\/h[1-2]>/'; 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);

    reply
    0
  • P粉394812277

    P粉3948122772024-04-02 09:09:58

    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.

    reply
    0
  • Cancelreply