正则表达式惰性匹配不匹配贪婪表达式匹配的字符串
Regex lazy matching does not match string matched by greedy expression
假设我有以下文本文件(名为 test.txt
):
\centerline{\includegraphics[width=0.50 \columnwidth]{example_graph.pdf}}
\centerline{\includegraphics[width=0.5 \columnwidth]{coarse_grain_illustration.png}}
\centerline{\includegraphics[width= 0.8 \columnwidth]{1y26_energy_dists.pdf}}
如果我使用贪心匹配进行搜索,我会得到预期的结果:
[user@host test]$ grep '\includegraphics.*df' test.txt
\centerline{\includegraphics[width=0.50 \columnwidth]{example_graph.pdf}}
\centerline{\includegraphics[width= 0.8 \columnwidth]{1y26_energy_dists.pdf}}
===========================================================================================================
但是,如果我使用惰性求值,我得不到任何结果:
[user@host test]$ grep '\includegraphics.*?df' test.txt
===========================================================================================================
什么给了?为什么使用惰性评估与本质上相同的模式不匹配?
.*?
或惰性模式并非处处支持。您将不得不在 perl 模式下使用 grep -P
或 grep 来实现它。
假设我有以下文本文件(名为 test.txt
):
\centerline{\includegraphics[width=0.50 \columnwidth]{example_graph.pdf}}
\centerline{\includegraphics[width=0.5 \columnwidth]{coarse_grain_illustration.png}}
\centerline{\includegraphics[width= 0.8 \columnwidth]{1y26_energy_dists.pdf}}
如果我使用贪心匹配进行搜索,我会得到预期的结果:
[user@host test]$ grep '\includegraphics.*df' test.txt
\centerline{\includegraphics[width=0.50 \columnwidth]{example_graph.pdf}}
\centerline{\includegraphics[width= 0.8 \columnwidth]{1y26_energy_dists.pdf}}
===========================================================================================================
但是,如果我使用惰性求值,我得不到任何结果:
[user@host test]$ grep '\includegraphics.*?df' test.txt
===========================================================================================================
什么给了?为什么使用惰性评估与本质上相同的模式不匹配?
.*?
或惰性模式并非处处支持。您将不得不在 perl 模式下使用 grep -P
或 grep 来实现它。