from typing import Optional
import re


def search_via_regex(text_in: str, pattern: str) -> Optional[dict]:
    """Search pattern using regular expression (ignore case)
    and return dict with 'text_before', 'text_match', 'text_after' or None"""
    if not text_in:
        return None
    pattern = r'(?P<before>^|\W)(?P<match>' + pattern + r')(?P<after>\W|$)'
    search_res = re.search(pattern, text_in, flags=re.IGNORECASE)
    if not search_res:
        return None
    # first match
    res_span = search_res.span()
    text_before = text_in[:res_span[0]] + search_res.group("before")
    text_match = search_res.group("match")
    text_after = search_res.group("after") + text_in[res_span[1]:]
    # parts = re.split(r"\s+", text_match)
    return {
        'text_before': text_before,
        'text_match': text_match,
        'text_after': text_after,
    }