2

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

part1-part2-part3.html

在哪里

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

例如,一个有效的 url 将是:

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$/

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

提前致谢。

4

3 回答 3

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$

在这里播放

于 2012-08-02T10:05:00.257 回答
1

试试这个:

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

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

\b[a-zA-Z]+-(?!\d+-)\w{2,}-\d{1,10}\.html\b
于 2012-08-02T10:18:23.407 回答
1

为了匹配 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
----------------------------------------------------------------------
于 2012-08-02T12:05:59.400 回答