正则表达式:正前瞻和负后瞻

问题描述 投票:0回答:1

问题:

如何在下面的 xml 中构建一个表达式来查找给定属性值“ServiceBController”的 bean id“serviceB”?

XML:

<bean id="serviceA"
    class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/ejb/ServiceAController</value>
    </property>
</bean>

<bean id="serviceB"
    class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/ejb/ServiceBController</value>
    </property>
</bean>

<bean id="serviceC"
    class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/ejb/ServiceCController</value>
    </property>
</bean>

我尝试过的:

此模式具有正向前瞻以确保匹配在

id="
之前进行,然后是正向前瞻以确保匹配在
ServiceBController
之前进行。但这也匹配“serviceA”。

(?<=id=\")[^\"]+(?=[^\f]+ServiceBController)

https://regex101.com/r/G6KfM2/1

我也尝试过对“serviceA”的结束 xml 标记进行负向后查找,但我不确定如何构造这个表达式。我想说的是:比赛必须以

ServiceBController
进行,但不能包含
/bean

(?<=id=\")[^\"]+(?=[^\f]+ServiceBController(?

https://regex101.com/r/G6KfM2/2

regex regex-lookarounds
1个回答
0
投票

我会做什么::

$ xmllint --xpath 'string(//bean[@id="serviceB"])' file | perl -pe 's/\s+//g'
java:comp/env/ejb/ServiceBControllers
© www.soinside.com 2019 - 2024. All rights reserved.