如何匹配MySQL重定向表中存储的正则表达式?
Got it, let's solve this problem step by step. Here's how you can check if an incoming URI matches any of the regex patterns in your redirect table and return the corresponding entry:
Approach
The core idea is to iterate through each entry in your redirect table, compile the stored regex pattern, and verify if the input URI fully matches that pattern. The first matching entry is returned (if you have overlapping patterns, make sure more specific ones come first in the table to avoid unexpected results).
Python Implementation
Let's start with a Python example, which has robust built-in regex support:
Step 1: Represent the Redirect Table
First, translate your table into a easy-to-work-with data structure—like a list of dictionaries:
redirect_table = [ {"id": 1, "uri": "^/foo$", "redirection": "https://www.google.com", "code": 301}, {"id": 2, "uri": "^/bar[0-9]$", "redirection": "https://facebook.com", "code": 301} ]
Step 2: Write the Matching Function
This function takes the input URI and the table, then checks each entry for a match:
import re def find_matching_redirect(input_uri, redirect_table): for entry in redirect_table: # Compile the regex pattern (add re.IGNORECASE if you need case-insensitive matches) regex = re.compile(entry["uri"]) # Use fullmatch to ensure the entire URI matches the pattern (aligns with ^ and $ anchors) if regex.fullmatch(input_uri): return entry # Return None if no matching entry is found return None
Step 3: Test the Function
Let's verify with your example and other common cases:
# Test 1: Exact match for /foo (your example) print(find_matching_redirect("/foo", redirect_table)) # Output: {'id': 1, 'uri': '^/foo$', 'redirection': 'https://www.google.com', 'code': 301} # Test 2: Match /bar followed by a digit (e.g., /bar7) print(find_matching_redirect("/bar7", redirect_table)) # Output: {'id': 2, 'uri': '^/bar[0-9]$', 'redirection': 'https://facebook.com', 'code': 301} # Test 3: No match (e.g., /foobar or /bar without a digit) print(find_matching_redirect("/bar", redirect_table)) # Output: None
Key Notes
- Regex Compilation: Compiling regexes upfront improves performance, especially if you're checking multiple URIs. For frequent use, pre-compile all patterns once instead of compiling them every time the function runs.
- Full Match: Using
fullmatchensures the entire URI matches the pattern, which aligns perfectly with the^(start) and$(end) anchors in your existing patterns. If you ever remove those anchors,fullmatchstill enforces a complete match. - Case Sensitivity: By default, matches are case-sensitive. To allow case-insensitive matches (e.g.,
/FOOmatching^/foo$), add there.IGNORECASEflag:re.compile(entry["uri"], re.IGNORECASE). - Pattern Order: If you have overlapping patterns (e.g.,
^/barand^/bar[0-9]$), place more specific patterns first so they're checked before broader ones.
JavaScript Alternative
If you're working in JavaScript, here's an equivalent implementation:
const redirectTable = [ {id: 1, uri: "^/foo$", redirection: "https://www.google.com", code: 301}, {id: 2, uri: "^/bar[0-9]$", redirection: "https://facebook.com", code: 301} ]; function findMatchingRedirect(inputUri) { for (const entry of redirectTable) { const regex = new RegExp(entry.uri); // Since your patterns include ^ and $, test() will check for a full match if (regex.test(inputUri)) { return entry; } } return null; } // Test cases console.log(findMatchingRedirect("/foo")); // Returns first entry console.log(findMatchingRedirect("/bar9")); // Returns second entry console.log(findMatchingRedirect("/foobar")); // Returns null
内容的提问来源于stack exchange,提问作者Hammerbot

