可变顺序正则表达式语法 [英] Variable order regex syntax

查看:94
本文介绍了可变顺序正则表达式语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法表明两个或更多的正则表达式短语可以以任何顺序出现?例如,XML属性可以按任意顺序编写。假设我有以下XML:

 < a href =home.phpclass =linktitle =家>家庭和LT; / A> 
< a href =home.phptitle =Homeclass =link>主页< / a>

我会如何编写一个匹配来检查类和标题,并且适用于这两种情况?我主要寻找允许我按任何顺序检查的语法,而不仅仅是匹配类和标题,因为我可以这样做。除了包含两种组合并将它们连接到|之外,还有什么办法吗?



编辑:我的首选是在单个正则表达式中完成它,因为我正在以编程方式构建它并进行单元测试。不,我相信用单个RE完成它的最好方法就是你所描述的。不幸的是,当你的XML可以有5个不同的属性时,它会变得非常混乱,给你一个大量的不同的RE来检查。



另一方面,我不会用RE做这个,因为它们不是编程语言。使用XML处理库的旧式方法有什么问题?



如果您需要 来使用RE,没什么帮助,但我相信在工作中使用正确的工具。

Is there a way to indicate that two or more regex phrases can occur in any order? For instance, XML attributes can be written in any order. Say that I have the following XML:

<a href="home.php" class="link" title="Home">Home</a>
<a href="home.php" title="Home" class="link">Home</a>

How would I write a match that checks the class and title and works for both cases? I'm mainly looking for the syntax that allows me to check in any order, not just matching the class and title as I can do that. Is there any way besides just including both combinations and connecting them with a '|'?

Edit: My preference would be to do it in a single regex as I'm building it programatically and also unit testing it.

解决方案

No, I believe the best way to do it with a single RE is exactly as you describe. Unfortunately, it'll get very messy when your XML can have 5 different attributes, giving you a large number of different REs to check.

On the other hand, I wouldn't be doing this with an RE at all since they're not meant to be programming languages. What's wrong with the old fashioned approach of using an XML processing library?

If you're required to use an RE, this answer probably won't help much, but I believe in using the right tools for the job.

这篇关于可变顺序正则表达式语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆