在python中使用re(正则表达式)获取整个路径字符串

在python中使用re(正则表达式)获取整个路径字符串,python,regex,Python,Regex,这是我的字符串: str=”“”\n\n\n\n\n true\n true\n\n\n\n 594eb06a-5e1f-4577-b005-dd21c07a9979\n 12.1.3.0.1\n\n\n”“” 我必须获取字符串包含的整个字符串oramds: 输出应为: oramds:/apps/Common/Events/EventHandlerDefinition.edl 我正在使用: MDS = re.search(r'\b(\w*oramds\w*)\b', str(search_st

这是我的字符串:

str=”“”\n\n\n\n\n true\n true\n\n\n\n 594eb06a-5e1f-4577-b005-dd21c07a9979\n 12.1.3.0.1\n\n\n”“”

我必须获取字符串包含的整个字符串
oramds

输出应为:

oramds:/apps/Common/Events/EventHandlerDefinition.edl
我正在使用:

MDS = re.search(r'\b(\w*oramds\w*)\b', str(search_str))
    print(MDS)
但它只给了我
['oramds']

我想在字符串中列出以
oramds:
开头的整个路径。

只需替换即可

MDS=re.search(r'\b(\w*oramds\w*)\b',str(search\u str))

MDS=re.search(r'\b(oramds:\S+\b',str(search\u str))
它应该可以正常工作


编辑

我刚刚注意到您只是简单地打印了
MDS
,但是当您使用搜索时,您需要使用
object.group()
来获得匹配的表达式。如果要避免使用
group()
,则可以使用
re.findall()
提取匹配的表达式。

因此,您的新代码将

重新导入
search_str=“”\n\n\n\n\n true\n true\n\n\n\n 594eb06a-5e1f-4577-b005-dd21c07a9979\n 12.1.3.0.1\n\n\n”“”
MDS=re.search(r'\b(oramds:\S+\b',str(search\u str))
打印(MDS.group())

但是,您可以考虑阅读这篇文章来更好地理解Python中正则表达式的概念。
希望这能解决您的问题。

它将只匹配
或MDS
,因为
\w
将匹配单词字符,而不会匹配点或正斜杠等

字符串以
“oramds:
开头,而不是使用单词边界
\b
,因此您也可以使用
[^”]+
匹配到结束双引号,并使用捕获组匹配内容

"(oramds:[^"]+)"
|

比如说

import re
search_str = """<import namespace="http://schemas.oracle.com/events/edl/EventHandlerDefinition" location="oramds:/apps/Common/Events/EventHandlerDefinition.edl" importType="edl"/>\n   <import namespace="http://xmlns.oracle.com/pcbpel/adapter/db/GSKICDSApplication/GSKInformICDSSub/getEnrollmentNumDB" location="WSDLs/getEnrollmentNumDB.wsdl" importType="wsdl"/>\n   <import namespace="http://xmlns.oracle.com/pcbpel/adapter/db/GSKICDSApplication/GSKInformICDSSub/getSitenuemonics" location="WSDLs/getSitenuemonics.wsdl" importType="wsdl"/>\n   <service name="gskTransactions" ui:wsdlLocation="WSDLs/gskTransactions.wsdl">\n      <interface.wsdl interface="http://xmlns.oracle.com/pcbpel/adapter/jms/ICDSApplicationSOA/GSKInformICDSSub/gskTransactions#wsdl.interface(Consume_Message_ptt)"/>\n      <binding.jca config="Adapters/gskTransactions_jms.jca">\n         <property name="useRejectedMessageRecovery" type="xs:string" many="false" override="may">true</property>\n        <property name="singleton" many="false">true</property>\n      </binding.jca>\n   </service>\n   <property name="compositeID" type="xs:string" many="false">594eb06a-5e1f-4577-b005-dd21c07a9979</property>\n   <property name="productVersion" type="xs:string" many="false">12.1.3.0.1</property>\n   <component name="GSKInformICDSSub" version="2.0">\n      <implementation.bpel src="BPEL/GSKInformICDSSub.bpel"/>\n      <componentType>\n         <service name="gskinformicdssub_client" ui:wsdlLocation="WSDLs/gskTransactions.wsdl">\n"""

MDS = re.search(r'"(oramds:[^"]+)"', str(search_str))
print(MDS.group(1))

我认为char/不属于\w,所以它停止了there@Davіd您的表达式给了我till oramds:/apps/Common/Events/eventhandler定义您的表达式给了我till oramds:/apps/Common/Events/eventhandler定义我还想要.edl扩展名好的,当我在re.findall中使用此表达式时,我收到了感谢。谢谢您的表达式给了我me till oramds:/apps/Common/Events/EventHandlerDefinition我想在oramds:/apps/Common/Events/EventHandlerDefinition.edlI获得所需的输出。当我打印结果时,它会显示
.edl
extension@ShrikantGourh我已经对我的回答进行了必要的澄清,以便您更好地理解。
oramds:/apps/Common/Events/EventHandlerDefinition.edl