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_-]+/

今、私は次のような条件を追加するために手を必要とします:

テキストに /K/ が最大で 1 ~ 2 回出現する場合は、これに一致します。

前もって感謝します。

よろしくお願いします。

ホセマ

4

2 に答える 2

1

このREは、1または2 /K/を持つ/F/ [0-9] {1,2}以降のすべてに一致します。また、 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}$
于 2010-05-05T11:34:44.853 に答える
1

これをすべて1行で行う必要がありますか?

私が取るアプローチは、正規表現を実行してから、/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
}

アップデート

この正規表現は、最初の 2 つの K までのすべてに一致し、その後は filename.html という URL のみを許可します。

^http://sub.domain.com/selector/F/[\d]+/[a-zA-Z]+/[\d]+/[a-zA-Z]+/[\d]+/K/[a-zA-Z_]+\.html$
于 2010-05-05T11:24:06.747 に答える