简体   繁体   English

XML 中的正则表达式使用 PHP:找到某个 xml 标记的某个值

[英]Regex in XML using PHP: find certain value of a certain xml tag

I am trying to get the value of a certain attribute in a certain xml tag with regex but cant get it right, maybe someone has an idea how to do it?我正在尝试使用正则表达式获取某个 xml 标记中某个属性的值,但无法正确处理,也许有人知道该怎么做?

The xml looks like this: xml 看起来像这样:

<OTA_PingRQ>
  <Errors>
    <Error Code="101" Type="4" Status="NotProcessed" ShortText="Authentication refused">Authentication : login failed</Error>
  </Errors>
</OTA_PingRQ>

and id like to match only the value of the Shorttext inside the Error tag.并且 id 喜欢只匹配 Error 标签内的 Shorttext 的值。 in the end it should give me "Authentication refused" back.最后它应该给我"Authentication refused"

What ive tried so far is using a lookbehind and lookahead, which doesnt let me take quantifiers with non fixed width.到目前为止,我尝试的是使用后向和前瞻,这不允许我采用非固定宽度的量词。 Like that (?<=<Error.).*?(?=>) .像那样(?<=<Error.).*?(?=>) Can someone tell me how to only match the value of the shorttext (inside the error tag)?有人能告诉我如何只匹配短文本的值(在错误标签内)吗?

You didn't specify the language you're using, i can give you the solution with PHP, the regex remain the same in every language anyway.你没有指定你正在使用的语言,我可以用 PHP 给你解决方案,反正每种语言的正则表达式都是一样的。

Here is the regex you're looking for:这是您正在寻找的正则表达式:

#\<Error Code\=\"[0-9]+\" Type\=\"[0-9]+\" Status\=\"NotProcessed\" ShortText\=\"([a-z 0-9]+)\"\>#is

Concrete PHP use:具体 PHP 用途:

$yourOriginalString = '
<OTA_PingRQ>
  <Errors>
    <Error Code="101" Type="4" Status="NotProcessed" ShortText="Authentication refused">Authentication : login failed</Error>
  </Errors>
</OTA_PingRQ>' ;

preg_match_all('#\<Error Code\=\"[0-9]+\" Type\=\"[0-9]+\" Status\=\"NotProcessed\" ShortText\=\"([a-z 0-9]+)\"\>#im', $yourOriginalString, $result) ;
print_r($result) ;

the regex function will return an array with:正则表达式 function 将返回一个数组:

   [0] => Array
        (
            [0] => <Error Code="101" Type="4" Status="NotProcessed" ShortText="Authentication refused">
        )

    [1] => Array
        (
            [0] => Authentication refused
        )

[0] is the full match [1] list the content in the matching capturing groups: each () set in your regex [0] 是完全匹配 [1] 列出匹配捕获组中的内容:每个 () 在您的正则表达式中设置

Some Regex explication:一些正则表达式解释:

Type\=\"[0-9]+\"

Assume "Type" can change and be any numbers.假设“类型”可以改变并且是任何数字。

 ShortText\=\"([a-z 0-9]+)\"

Catch a string alphanumeric + space string.捕获字符串字母数字 + 空格字符串。 If you need some other stuffs, you can update like:如果你需要一些其他的东西,你可以像这样更新:

*[a-z 0-9\!\-]+*

catch !抓住 ! and - too和 - 太

#is

Are flags and ignore = caps and line break是标志和忽略 = 大写和换行符

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM