如何用正则表达式提取查询字符串中word.EXACT包裹的目标内容?
word.EXACT Formatted Strings Got it, let's tackle this problem step by step. You need to pull out the content wrapped inside any word.EXACT-style patterns (case-insensitive, like MESH.EXACT, words.exact, etc.) from a given query string—whether the content is quoted or not.
Extraction Result for Your Input Query
First, here's the extracted content from your provided query:
"insulin treatment""blood glucose monitoring"
How to Implement This
The most straightforward way to handle this is using a regular expression that targets the pattern structure. Here's a breakdown of the approach:
Regex Pattern Explanation
We can use this case-insensitive regex to match and capture the inner content:
[a-zA-Z]+\.EXACT\((.*?)\)
Let's break down what each part does:
[a-zA-Z]+: Matches the prefix word (likeEMB,MESH,words) regardless of case\.EXACT: Matches the literal.EXACT(the backslash escapes the dot, since dots have special meaning in regex)\(: Matches the opening parenthesis(.*?): Non-greedy capture group that grabs everything inside the parentheses until it hits the closing)(this preserves quotes if they're present)\): Matches the closing parenthesis
Example Code (Python)
Here's a quick Python script that implements this:
import re # Your input query query = '(EMB.EXACT("insulin treatment")) and (MESH.EXACT("blood glucose monitoring")) OR "Self-Monitoring of Blood Glucose”' # Case-insensitive regex pattern pattern = r'[a-zA-Z]+\.EXACT\((.*?)\)' matches = re.findall(pattern, query, re.IGNORECASE) # Output the results print("Extracted content:") for idx, content in enumerate(matches, 1): print(f"{idx}. {content}")
This script will output exactly the results we listed earlier, and it works for all your example cases too—whether the inner content is quoted (like "blood glucose monitoring") or unquoted (like N-Words).
内容的提问来源于stack exchange,提问作者Moka

