简体   繁体   English

如何在python中使用正则表达式提取字符串旁边的单词

[英]How to extract words next to a string using regex in python

9.DATUM DER ERTEILUNG DER ZULASSUNG/VERLÄNGERUNG DER ZULASSUNG
10.STAND DER INFORMATION
Juni 2019
Rezeptpflicht/Apothekenpflicht
Rezept- und apothekenpflichtig, wiederholte Abgabe verboten.

This is my text and I am trying to extract dates which are always after STAND DER INFORMATION .这是我的文本,我试图提取始终在STAND DER INFORMATION之后的日期。 Juni 2019 in this example text above. Juni 2019在上面的示例文本中。

I have tried string split method but that doesn't work for me as I just need the dates.我尝试过字符串拆分方法,但这对我不起作用,因为我只需要日期。

If your text has STAND DER INFORMATION prior to date as illustrated you can use the following.如果您的文本在日期之前具有STAND DER INFORMATION ,如图所示,您可以使用以下内容。

Code代码

import re
re.findall(r'(?<=STAND DER INFORMATION\s)\D{3,4}\s\d{4}', s, re.MULTILINE)

Explanation解释

# s is text string
# <=STAND DER INFORMATION\n - look behind for STAND DER INFORMATION followed by \n
# \D is non-digit (so 3 or 4 non-digits)
# \d digits (so four digit date)
# re.MULTILINE - multiline flag to allow matches across multiple lines

Test测试

s = """9.DATUM DER ERTEILUNG DER ZULASSUNG/VERLÄNGERUNG DER ZULASSUNG
10.STAND DER INFORMATION
Juni 2019
Rezeptpflicht/Apothekenpflicht
Rezept- und apothekenpflichtig, wiederholte Abgabe verboten."""
dates = re.findall(r'(?<=STAND DER INFORMATION\n)\D{3,4}\s\d{4}', s, re.MULTILINE)
print(dates)

Output输出

['Juni 2019']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当在 Python 中使用正则表达式之间有单词时,如何提取特定关键字之后的下一行? - How to extract the next line after a specific keyword when there are words in between using regex in Python? 在Python中使用正则表达式从字符串中提取具有特定字符的单词列表 - Extract list of words with specific character from string using regex in Python python 的正则表达式:如何在单词之间提取字符串? - Regex for python: how do I extract a string between words? 如何使用正则表达式从冒号前的字符串中提取单词并在 python 中排除 \n - How can i extract words from a string before colon and excluding \n from them in python using regex 如何使用正则表达式在python中提取关键字列表后的单词? - how to extract words following a list of keywords in python using regex? 如何在python中使用单个正则表达式从tweet中提取所有单词? - How to extract all words from tweet using single regex in python? 如何使用 python 中的正则表达式从字节中提取单词? - How to extract words from bytes using regex in python? 如何在 Python 中使用正则表达式提取格式化的字符串? - How to extract a formatted string using regex in Python? 如何提取 python 字符串中的单词 - How To extract words in a python string python使用正则表达式提取大写单词 - python extract capitalized words using regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM