以非贪婪的方式使用匹配替换文本?

发布于 2024-11-15 10:17:47 字数 1283 浏览 0 评论 0原文

我有一个 SOAP 调用,如下所示:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:FileserveAPI" xmlns:types="urn:FileserveAPI/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><tns:login><username xsi:type="xsd:string">REPLACEME</username><password xsi:type="xsd:string">REPLACEMETOO</password></tns:login></soap:Body></soap:Envelope>

我想用定义的值替换 REPLACEMEE 和 REPLACEMETOO,但在主题中这些值可以是任何值。

我尝试了这些 preg_match 规则:

$body = preg_replace('#>.+?</username>#','>myuser</username>',$body);
$body = preg_replace('#>.+?</password>#','>mypw/username>',$body);

但我最终得到了这个字符串:

<?xml version="1.0" encoding="utf-8"?>myuser</username></tns:login></soap:Body></soap:Envelope>

我不明白为什么?

为什么会发生这种情况?我需要如何修改我的规则?

编辑:

我已经用诸如 [^>]+ 这样的否定字符类解决了这个问题,但是我仍然对为什么非贪婪不起作用感兴趣。

I have a SOAP Call that looks like this:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:FileserveAPI" xmlns:types="urn:FileserveAPI/encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><tns:login><username xsi:type="xsd:string">REPLACEME</username><password xsi:type="xsd:string">REPLACEMETOO</password></tns:login></soap:Body></soap:Envelope>

I want to replace REPLACEMEE and REPLACEMETOO with defined values, but in the subject those values can be anything.

I tried theese preg_match rules:

$body = preg_replace('#>.+?</username>#','>myuser</username>',$body);
$body = preg_replace('#>.+?</password>#','>mypw/username>',$body);

But I end up with this string:

<?xml version="1.0" encoding="utf-8"?>myuser</username></tns:login></soap:Body></soap:Envelope>

I cant understand why?

Why is this occouring and how do I need to modify my rules?

Edit:

I already worked my way around it with a negated character class like [^>]+ however I am still intersted in why the non greedyness doesnt work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-11-22 10:17:47

“非贪婪”按预期工作。

第一个正则表达式在遇到 > 时启动,然后仅采用最小值,直到到达第一个

如果 XML 是:

<?xml version="1.0"><accnt1><username>foo</username></accnt1><accnt2><username>bar</username></accnt2> ...

然后 >.+? 抓取:

><accnt1><username>foo</username>

>.+ 会抓取:

><accnt1><username>foo</username></accnt1><accnt2><username>bar</username>

The "non greedyness" works as expected.

The first regex started when it encountered > and then took only the minimum until the first </username> was reached.

If the XML had been:

<?xml version="1.0"><accnt1><username>foo</username></accnt1><accnt2><username>bar</username></accnt2> ...

Then >.+?</username> grabs:

><accnt1><username>foo</username>

Whereas >.+</username> would grab:

><accnt1><username>foo</username></accnt1><accnt2><username>bar</username>
无可置疑 2024-11-22 10:17:47

尝试:

$body = preg_replace("/(<username[^>]+>).+?(<\/username>)/", "$1new value$2", $input);
$body = preg_replace("/(<password[^>]+>).+?(<\/password>)/", "$1new value..$2", $input);

Try:

$body = preg_replace("/(<username[^>]+>).+?(<\/username>)/", "$1new value$2", $input);
$body = preg_replace("/(<password[^>]+>).+?(<\/password>)/", "$1new value..$2", $input);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文