preg_match_all 解析类似 xml 的属性字符串
我有一个像这样的字符串:
option_alpha="value" option_beta="some other value" option_gamma="X" ...etc.
我用它来将它们解析为 name &值对:
preg_match_all("/([a-z0-9_]+)\s*=\s*[\"\'](.+?)[\"\']/is", $var_string, $matches)
哪个工作正常,除非遇到空属性值:
option_alpha="value" option_beta="" option_gamma="X"
我在正则表达式中做错了什么?
I have a string like so:
option_alpha="value" option_beta="some other value" option_gamma="X" ...etc.
I'm using this to parse them into name & value pairs:
preg_match_all("/([a-z0-9_]+)\s*=\s*[\"\'](.+?)[\"\']/is", $var_string, $matches)
Which works fine, unless it encounters an empty attribute value:
option_alpha="value" option_beta="" option_gamma="X"
What have I done wrong in my regex?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
应为
*
而不是+
。第一个意味着前一个表达式的任何出现次数都可以为零(因此可以省略它,这就是您所需要的)。后者意味着必须至少有一个。should be
*
instead of+
. The first means there can be zero to whatever occurrences of the previous expression (so it can be omitted, that is what you need). The latter means, there has to be at least one.我认为您想将表达式的中间部分从
(.+?)
更改为(.*?)
。这使得它成为对任何字符(包括无字符)的非贪婪匹配,而不是对至少一个字符的非贪婪匹配。I think you want to change the very middle of your expression from
(.+?)
to(.*?)
. That makes it a non-greedy match on any character (including no characters), instead of a non-greedy match on at least one character.这里的其他答案是正确的,因为您需要更改表达式的中间,但我会将其更改为 [^\"\']* ,这意味着“任何不是”的字符,0次或多次。这确保贪婪不符合预期,并且允许
您的表达式变为 空“”。
"/([a-z0-9_]+)\s*=\s*[\"\'][^\"\']*[\"\']/is"
注意您可以更改 [a- z0-9_] 到 [\w_],这也适用于大写字符。
The other answers here are right in that you need to change the middle of the expression, but I would change it to [^\"\']* which means "any character that is not a ", 0 or more times. This ensures the greediness doesn't match more than it is supposed to and allows for empty "".
your expression becomes
"/([a-z0-9_]+)\s*=\s*[\"\'][^\"\']*[\"\']/is"
note you can change the [a-z0-9_] to [\w_] which would also for upper case characters.