正则表达式量词问题

发布于 2024-08-31 01:33:00 字数 770 浏览 2 评论 0原文

我试图找到与此类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2024-09-07 01:33:00

您需要将所有这些都放在一行中吗?

我采取的方法是对 /K/ 执行正则表达式,然后计算得到的匹配数。

我认为 Boost 是一个 C++ 库,对吗?在 C# 中,我会这样做:

string url = "http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html";
if (Regex.Matches(url, "/K/").Count <= 2)
{
    // good url found
}

UPDATE

这个正则表达式将匹配前两个 K 之前的所有内容,然后只允许 url filename.html 之后:

^http://sub.domain.com/selector/F/[\d]+/[a-zA-Z]+/[\d]+/[a-zA-Z]+/[\d]+/K/[a-zA-Z_]+\.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:

string url = "http://sub.domain.com/selector/F/13/K/100546/sampletext/654654/K/sampletext_sampletext.html";
if (Regex.Matches(url, "/K/").Count <= 2)
{
    // good url found
}

UPDATE

This regex would match everything up to the first two K's and then only allow the url filename.html after that:

^http://sub.domain.com/selector/F/[\d]+/[a-zA-Z]+/[\d]+/[a-zA-Z]+/[\d]+/K/[a-zA-Z_]+\.html$
别想她 2024-09-07 01:33:00

此 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

^http://sub\.domain\.com/selector/F/[0-9]{1,2}(?:/K(?=/)(?:(?!/K/)/[a-z0-9_.-]+)*){1,2}$

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 :

^http://sub\.domain\.com/selector/F/[0-9]{1,2}(?:/K(?=/)(?:(?!/K/)/[a-z0-9_.-]+)*){1,2}$
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文