帮助使用 Actionscript 3 正则表达式和 xmpp 节
我有一个字符串:
<presence to="[email protected]/d9ec56e4"
from="[email protected]/testsubject_2">
<x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="none"
role="visitor"/></x></presence>
我如何找出它是否包含存在属性以及它来自谁? 我尝试过使用 as3 的 xml 方法,但它们要求 xml 是一个完整的文档,所以我想使用正则表达式,但已经晚了,我在睡觉前丢失了如此糟糕的帖子:)
if (string contains <presence to="){
// get who its from
}
i have a string:
<presence to="[email protected]/d9ec56e4"
from="[email protected]/testsubject_2">
<x xmlns="http://jabber.org/protocol/muc#user"><item affiliation="none"
role="visitor"/></x></presence>
how would i find out if it contains the presence attribute as well as who its from?
ive tried using as3's xml methods but they require that the xml is a complete document so i figure to use regex but its late and im lost so ill post before i go to sleep :)
if (string contains <presence to="){
// get who its from
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊啊!我强烈建议您不要使用正则表达式来解析 XML,这就是我们拥有 XML 解析器的原因。
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XML.html
或者只使用众多 XMPP 库之一:
http://code.google。 com/p/as3xmpp/
http://code.google.com/ p/seesmic-as3-xmpp/
ARGH! I would highly recommend you don't use regex to parse XML, that's why we have XML parsers.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XML.html
Or just use one of the many XMPP libraries out there:
http://code.google.com/p/as3xmpp/
http://code.google.com/p/seesmic-as3-xmpp/
/<(\w+)(( to|from)=\"([^\"]+)\")+>.*/
/<(\w+)(( to|from)=\"([^\"]+)\")+>.*</\1>/
搜索:
/<]*\sto\s*=)[^><]*\sfrom\s*=\s*["' ]([^><"']*)["'].*?/gis
$1
将引用其来源,例如[email protected]/testsubject_2
即使
to
/from
以不同的顺序。即使 XML 使用单引号,它也可以工作,但单引号之间不能有单引号。我可以进一步改进这种行为,但正则表达式会变得更长。
如果考虑到格式错误的 XML 文档,则不应使用正则表达式,而应使用 DOM 或 XPath。
Search for:
/<presence\b(?=[^><]*\sto\s*=)[^><]*\sfrom\s*=\s*["']([^><"']*)["'].*?</presence>/gis
$1
will refer to who its from, e.g.[email protected]/testsubject_2
It works even if
to
/from
in a different order.It works even if XML uses single quotes, but there must not be a single-quotes between single-quotes. I can improve this behaviour further, but the regex would become longer.
If you take malformed XML documents into account, you shouldn't use regex, use either DOM or XPath instead.