正则表达式量词问题
我试图找到与此类 URL 匹配的正则表达式:
http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html
并且不匹配:
http://sub.domain。 com/selector/F/13/K/10546/sampletext/5987/K/sample/K/101/sample_text.html
仅当 /K/ 的数量最小为 1 且最大为 2 (带有量词的东西)像{1,2})
直到这一刻我有以下正则表达式:
http://sub\.domain\.com/selector/F/[0-9]{1,2}/[a-z0-9_-]+/
现在我需要有人来添加任何类型的条件,例如:
如果文本中最多出现1到2次/K/,则匹配此条件。
提前致谢。
此致。
何塞马
Im trying to find a regular expression that matches this kind of URL:
http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html
and dont match this:
http://sub.domain.com/selector/F/13/K/10546/sampletext/5987/K/sample/K/101/sample_text.html
only if the number of /K/ is minimum 1 and maximum 2 (something with a quantifier like {1,2})
Until this moment i have the following regexp:
http://sub\.domain\.com/selector/F/[0-9]{1,2}/[a-z0-9_-]+/
Now i would need a hand to add any kind of condition like:
Match this if in the text appears the /K/ from 1 to 2 times at most.
Thanks in advance.
Best Regards.
Josema
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将所有这些都放在一行中吗?
我采取的方法是对
/K/
执行正则表达式,然后计算得到的匹配数。我认为 Boost 是一个 C++ 库,对吗?在 C# 中,我会这样做:
UPDATE
这个正则表达式将匹配前两个 K 之前的所有内容,然后只允许 url filename.html 之后:
Do you need to this all in one line?
The approach I would take is to do a regex for
/K/
and then count the number of matches I got.I think Boost is a C++ library right? In C# I would do it like this:
UPDATE
This regex would match everything up to the first two K's and then only allow the url filename.html after that:
此 RE 将匹配 /F/[0-9]{1,2} 之后具有 1 或 2 /K/ 的任何内容,它也可以匹配 http://sub.domain.com/selector/F/13/K /100546/stuff/21515/stuff/sampletext/654654/K/stuff/sampletext_sampletext.html:
This RE will match anything after the/F/[0-9]{1,2} that has 1 or 2 /K/, it could also match http://sub.domain.com/selector/F/13/K/100546/stuff/21515/stuff/sampletext/654654/K/stuff/sampletext_sampletext.html :