From 94903785d05b7b307132af9b1212f926204766bb Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Thu, 23 Jan 2025 20:40:33 +0100
Subject: [PATCH] [PR #9609/bf5c7f8b backport][stable-10] apache2-mod-proxy:
 better handling regexp extraction (#9616)

apache2-mod-proxy: better handling regexp extraction (#9609)

* apache2-mod-proxy: better handling regexp extraction

* add changelog frag

(cherry picked from commit bf5c7f8be886ec2a26d6eae35ef459e4908ee522)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
---
 changelogs/fragments/9609-apache2-mod-proxy-revamp4.yml | 2 ++
 plugins/modules/apache2_mod_proxy.py                    | 8 ++++----
 2 files changed, 6 insertions(+), 4 deletions(-)
 create mode 100644 changelogs/fragments/9609-apache2-mod-proxy-revamp4.yml

diff --git a/changelogs/fragments/9609-apache2-mod-proxy-revamp4.yml b/changelogs/fragments/9609-apache2-mod-proxy-revamp4.yml
new file mode 100644
index 0000000000..009a2a582d
--- /dev/null
+++ b/changelogs/fragments/9609-apache2-mod-proxy-revamp4.yml
@@ -0,0 +1,2 @@
+minor_changes:
+  - apache2_mod_proxy - better handling regexp extraction (https://github.com/ansible-collections/community.general/pull/9609).
diff --git a/plugins/modules/apache2_mod_proxy.py b/plugins/modules/apache2_mod_proxy.py
index fcd920fd7c..d2be6be612 100644
--- a/plugins/modules/apache2_mod_proxy.py
+++ b/plugins/modules/apache2_mod_proxy.py
@@ -220,14 +220,14 @@ else:
     HAS_BEAUTIFULSOUP = True
 
 # balancer member attributes extraction regexp:
-EXPRESSION = r"(b=([\w\.\-]+)&w=(https?|ajp|wss?|ftp|[sf]cgi)://([\w\.\-]+):?(\d*)([/\w\.\-]*)&?[\w\-\=]*)"
+EXPRESSION = re.compile(r"(b=([\w\.\-]+)&w=(https?|ajp|wss?|ftp|[sf]cgi)://([\w\.\-]+):?(\d*)([/\w\.\-]*)&?[\w\-\=]*)")
 # Apache2 server version extraction regexp:
-APACHE_VERSION_EXPRESSION = r"SERVER VERSION: APACHE/([\d.]+)"
+APACHE_VERSION_EXPRESSION = re.compile(r"SERVER VERSION: APACHE/([\d.]+)")
 
 
 def regexp_extraction(string, _regexp, groups=1):
     """ Returns the capture group (default=1) specified in the regexp, applied to the string """
-    regexp_search = re.search(string=str(string), pattern=str(_regexp))
+    regexp_search = _regexp.search(string)
     if regexp_search:
         if regexp_search.group(groups) != '':
             return str(regexp_search.group(groups))
@@ -251,7 +251,7 @@ class BalancerMember(object):
     """
 
     def __init__(self, management_url, balancer_url, module):
-        self.host = regexp_extraction(management_url, str(EXPRESSION), 4)
+        self.host = regexp_extraction(management_url, EXPRESSION, 4)
         self.management_url = str(management_url)
         self.protocol = regexp_extraction(management_url, EXPRESSION, 3)
         self.port = regexp_extraction(management_url, EXPRESSION, 5)