Problem: Use regular expressions to match text patterns to identify the longest possible match to find the shortest possible match
Workaround: Add after the * operator in the matching pattern. Modifier
ImportRe#Sample TextText ='computer says "No." Phone says "Yes."'#(a) Regex that finds quoted Strings-longest matchStr_pat = Re.compile (r'\"(.*)\"')Print(Str_pat.findall (text))#(b) Regex that finds quoted Strings-shortest matchStr_pat = Re.compile (r'\"(.*?) \"')Print(Str_pat.findall (text))
>>> ================================ RESTART ================================>>> [' No. " Phone says "Yes. ' ['No.'yes. ' ]
(a) The example is incorrectly matched into 2 quoted strings
Add: This section refers to an issue that you will encounter when writing a regular expression that contains a period (.) character.
In pattern matching, a period can match any character except a newline character.
"Python Cookbook" "String and Text" 7. Define a regular expression that implements the shortest match