out+=(f'Message: {re.sub(rf'(<[^>]*>) ({searchterm})', grabsearch.replaceoutsidetags,…
- User
out+=(f'Message: {re.sub(rf'(<[^>]*>)|({search_term})', grab_search.replace_outside_tags, (''+grab_server.escapeMessage(bytes(message).decode("utf-8"),id1,(board))+'' if isinstance(message,memoryview) else "---")
How can I match the full search_term and also a part of the search_term (like "ok das" in "wie geht das? aber wenn wir das sehen, geht das auch ganz OK. ok das wa alles"
- Assistant
To match both the full
search_termand its constituent words (like matching "ok das", "ok", and "das" in your text), you can construct a regular expression that includes both the full term and each individual word. Here's how you can modify your code:import re # Assume search_term is defined search_term = 'ok das' # Escape special regex characters in the search_term and split into words search_term_escaped = re.escape(search_term) search_words_escaped = [re.escape(word) for word in search_term.split()] # Construct the regex pattern to match HTML tags or the search terms pattern = rf'(<[^>]*>)|({search_term_escaped}|{"|".join(search_words_escaped)})' # Use the pattern in re.sub out += f'Message: {re.sub(pattern, grab_search.replace_outside_tags, (...) )}'