【问题标题】:Improve regular expression to match an url containing an alphanumeric word that has at least N letters改进正则表达式以匹配包含至少 N 个字母的字母数字单词的 url
【发布时间】:2012-08-02 20:05:59
【问题描述】:

我已经完成了一个正则表达式来匹配下一个模式的 url:

part1-part2-part3.html

在哪里

part1:是常用词
part2:是一个带下划线的字母数字单词,至少包含 2 个字母
part3:是一个数字词,有1到10位数字

例如,一个有效的网址是:

news-my_news_title_200_is-12345.html

所以
第 1 部分 = 新闻
第 2 部分 = my_news_title_200_is
第 3 部分 = 12345

我来了:

/^[a-z]+-([a-z0-9_]*(?=[a-z]{2,})[a-z0-9_]*).-([0-9]{1,10})\.html$/

用类表示:

/^\w+-([\w\d_]*(?=\w{2,})[\w\d_]*).-(\d{1,10})\.html$/

但我想有更好的方式来表达 R.E. 的第 2 部分。模式。

提前致谢。

【问题讨论】:

  • 一个明显的节省是使用快捷方式而不是范围,因此\w 用于字母数字字符,\d 用于数字字符等
  • 我知道,但我指的是第 2 部分中的前瞻表达式
  • 2个字母要不要连续?

标签: regex lookahead


【解决方案1】:

试试这个

\b(?is)[a-z]+-\w*(?=[a-z]{2,})\w*-[0-9]{1,10}\.html\b

^(?is)[a-z]+-\w*(?=[a-z]{2,})\w*-[0-9]{1,10}\.html$

here

【讨论】:

  • \w{2,} 也应该包含数字和下划线
  • @FeidaKila:哦!我错过了。更新了,测试一下。
  • 没关系,谢谢你的链接。和我的一样,但更清楚。有没有办法更简单地表达这个 '\w*(?=[a-z]{2,})\w*'?
  • 也许你可以在没有任何lookahead 的情况下使用[0-9a-z_]*[a-z]{2,}[0-9a-z_]*
  • 当然,但如果我不希望字母是强制连续的
【解决方案2】:

试试这个:

\b[a-zA-Z]+-\w{2,}-\d{1,10}\.html\b

更强(避免第 2 部分只匹配数字):

\b[a-zA-Z]+-(?!\d+-)\w{2,}-\d{1,10}\.html\b

【讨论】:

  • \w{2,} 不包含数字和下划线
  • 如果你担心这个问题,请看我的最新修改。
【解决方案3】:

为了匹配 2 个不连续的字母,您可以这样做(perl 中给出的示例,但它适用于理解 PCRE 的语言)

use strict;
use warnings;
use 5.010;
use YAPE::Regex::Explain;

my $re = qr/^\w+-(\w*?[a-z]+\w*?[a-z]+\w*?)-\d+\.html$/;
say YAPE::Regex::Explain->new( $re )->explain;
while(<DATA>) {
    chomp;
    say ($_ =~ $re ? "match : $_" : "not match : $_");
}
__DATA__
news-my_news_title_200_is-12345.html
news-m_200_s-12345.html
news-m_200-12345.html

输出:

match : news-my_news_title_200_is-12345.html
match : news-m_200_s-12345.html
not match : news-m_200-12345.html

正则表达式解释:

(?-imsx:^\w+-(\w*?[a-z]+\w*?[a-z]+\w*?)-\d+\.html$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  -                        '-'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \w*?                     word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the least amount
                             possible))
----------------------------------------------------------------------
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \w*?                     word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the least amount
                             possible))
----------------------------------------------------------------------
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \w*?                     word characters (a-z, A-Z, 0-9, _) (0 or
                             more times (matching the least amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  -                        '-'
----------------------------------------------------------------------
  \d+                      digits (0-9) (1 or more times (matching
                           the most amount possible))
----------------------------------------------------------------------
  \.                       '.'
----------------------------------------------------------------------
  html                     'html'
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

【讨论】:

  • 感谢发布此解决方案,可能值得其他用户使用,但不是我因为我需要它来进行 mod_rewrite
猜你喜欢
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2020-09-01
  • 2017-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多