在Python中使用正则表达式编译有什么好处吗?
h = re.compile('hello')
h.match('hello world')
Run Code Online (Sandbox Code Playgroud)
VS
re.match('hello', 'hello world')
Run Code Online (Sandbox Code Playgroud) 我有一个包含简短履历的行政文件数据集。我试图通过使用python和某些模式匹配来提取人们的年龄。句子的一些示例是:
这些是我在数据集中确定的一些模式。我想补充一点,还有其他模式,但是我还没有遇到它们,并且不确定如何实现。我编写了以下代码,效果很好,但是效率很低,因此在整个数据集上运行将花费太多时间。
#Create a search list of expressions that might come right before an age instance
age_search_list = [" " + last_name.lower().strip() + ", age ",
" " + clean_sec_last_name.lower().strip() + " age ",
last_name.lower().strip() + " age ",
full_name.lower().strip() + ", age ",
full_name.lower().strip() + ", ",
" " + last_name.lower() + ", ",
" " + last_name.lower().strip() + " \(",
" " + last_name.lower().strip() + " is "]
#for each element in …Run Code Online (Sandbox Code Playgroud)