开发者

Url rewriting a Regex help

开发者 https://www.devze.com 2023-01-01 16:23 出处:网络
What Regex do I need for match this url: Match: 1234 1234/ 1234/article-name Don\'t match: 1234absd 1234absd/article-name

What Regex do I need for match this url:

Match:

1234
1234/
1234/article-name

Don't match:

1234absd
1234absd/article-name
1234/article.aspx
1234/any.dot.in.the.url
开发者_如何转开发


You can try:

^\d+(?:\/[\w-]*)?$

This matches a non-empty sequence of digits at the beginning of the string, followed by an optional suffix of a / and a (possibly empty) sequence of word characters (letters, digits, underscore) and a -.

This matches (see on rubular):

1234
1234/
1234/article-name
42/section_13

But not:

1234absd
1234absd/article-name
1234/article.aspx
1234/any.dot.in.the.url
007/james/bond

No parenthesis regex

You shouldn't need to do this, but if you can't use parenthesis at all, you can always expand to alternation:

^\d+$|^\d+\/$|^\d+\/[\w-]*$


^\d+(/?)|(/[a-zA-Z-]+)$

That may work. or not. Hope it helps


Hope this ll help u ........

        string data = "1234/article-name";
        Regex Constant = new Regex("(?<NUMBERS>([0-9]+))?(//)?(?<DATA>([a-zA-Z-]*))?");
        MatchCollection mc;
        mc = Constant.Matches(data,0);
        if (mc.Count>0)
        {
            for (int l_nIndex = 0; l_nIndex < mc.Count; l_nIndex++)
            {
                string l_strNum = mc[l_nIndex].Groups["NUMBERS"].Value;
                string l_strData = mc[l_nIndex].Groups["DATA"].Value;
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消