I'm trying to write a regular expression in PHP. From this code I want to match 'bar'.
<data info="foo">
"bar"|tr
</data>
I tried this two regex, without success. It matches 'foo"> "bar'.
$regex = '/"(.*?)"\|tr/s';
$regex = '/"[^"]+(.*?)"\|tr/s';
Anyone can help me?
You need to escape the backslash in PHP strings:
$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. Regex is only recommendable in conjunction with a 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. Uses look-ahead .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.