![](/img/trans.png)
[英]Python extract 3 words before and 3 words after a specific list of words with a regex
[英]Python RegEx to get words after a specific string
有弦
string= """"$deletedFields":["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,3)","name":"Finance","$type":"voyager.identity.profile.Skill"},{"$deletedFields":["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,22)","name":"Financial ["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,34)","name":"Due Diligence","name":"Strategy""""
我可以使用哪種常規表達式來檢索“名稱”后的值:以獲得“盡職調查”,“財務”和“財務”
我努力了
match = re.compile(r'"name"\\:(.\\w+)') match.findall(string)
但它返回
['"Finance', '"Financial', '"Due', '"Financial', '"Strategy']
Due Diligence
已拆分,我希望兩個詞都為一個。
正則表達式無法檢測到您的空格,因為/w
僅搜索非特殊字符。
"name"\\:(.\\w+\\s*\\w*)
用一個額外的單詞說明任何可能的空格(對三個單詞不起作用,但是會根據您的情況而定)
"name"\\:(.\\w+\\s*\\w*"?)
占語錄"
在每一個結束,但沒有得到財務。 例
編輯:固定的第二個正則表達式為“金融
我會用非飢餓的.*?
帶有引號的表達式:
import re
string = """$deletedFields":["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,3)","name":"Finance","$type":"voyager.identity.profile.Skill"},{"$deletedFields":["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,22)","name":"Financial ["standardizedSkillUrn","standardizedSkill"],"entityUrn":"urn:li:fs_skill:(ACoAAAIv9SQBMzclPm3CZzL1QceTH5W0VrsdxbE,34)","name":"Due Diligence","name":"Strategy"""
# With the leading double quote
match = re.compile(r'"name"\:(".*?)["\[]')
a = match.findall(string)
print a
# Stripping out the leading double quote
match = re.compile(r'"name"\:"(.*?)["\[]')
b = match.findall(string)
print b
最終輸出是:
['"Finance', '"Financial ', '"Due Diligence']
['Finance', 'Financial ', 'Due Diligence']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.