简体   繁体   English

PHP正则表达式提取标记主体中的带引号的文本

[英]PHP regular expression to extract quoted text in tag body

I'm trying to write a regular expression in PHP. 我试图用PHP编写一个正则表达式。 From this code I want to match 'bar'. 从此代码中,我要匹配“ bar”。

<data info="foo">
  "bar"|tr
</data>

I tried this two regex, without success. 我尝试了这两个正则表达式,但没有成功。 It matches 'foo"> "bar'. 它匹配'foo“>” bar“。

$regex = '/"(.*?)"\|tr/s';
$regex = '/"[^"]+(.*?)"\|tr/s';

Anyone can help me? 有人可以帮助我吗?

You need to escape the backslash in PHP strings: 您需要在PHP字符串中转义反斜杠:

$regex = '/"([^"]*)"\\|tr/s';

I added a capturing group to get the contents of the quotes, which you seem to be interested in. 我添加了一个捕获组来获取报价内容,您似乎对此感兴趣。

Since you seem to apply the regex to XML, I just want to warn you that XML and regular expressions don't play well together. 因为您似乎将正则表达式应用于XML,所以我只想警告您XML和正则表达式不能很好地结合使用。 Regex is only recommendable in conjunction with a DOM. 仅建议将正则表达式与DOM结合使用。

\"\w+\"

应该匹配括号中的任何单词char

Try this: 尝试这个:

$regex = '/"([^">]+)"\|tr/s'

If you want to match just letters and numbers, you can do: 如果您只想匹配字母和数字,则可以执行以下操作:

$regex = '/"([\w\d]+)"\|tr/s'
$regex = '/"(.+?)"(?=\|tr)/'

Will match "bar" (including the quotes), and you have the bar string (without quotes) in $1. 将匹配"bar" (包括引号),并且$ 1中有bar字符串(不带引号)。 Uses look-ahead . 使用向前看

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

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