简体   繁体   English

匹配正则表达式中的可选斜杠

[英]Matching optional slash in regex

I need a regex that matches first two words between three "/" characters in url: eg. 我需要一个正则表达式,用于匹配网址中三个“ /”字符之间的前两个单词: in /en/help/test/abc/def it should match /en/help/. 在/ en / help / test / abc / def中应该与/ en / help /相匹配。

I use this regex: /.*?/(.*?)/ however sometimes I have the url without the last slash like /en/help which does not match because of the missing last slash. 我使用此正则表达式: /.*?/(.*?)/ /(.*?)/,但是有时我的网址没有最后一个斜杠,例如/ en / help,由于缺少最后一个斜杠而导致不匹配。

Can you help me to adjust the regex to match only "/en/help" part? 您能帮我调整正则表达式,使其仅匹配“ / en / help”部分吗? Thanks 谢谢

A simple way to solve it is to replace reluctant (.*?)/ with greedy ([^/]*) : 一种简单的解决方法是用贪婪([^/]*)替换勉强(.*?)/

/.*?/([^/]*)

This would stop at the third slash if there is one, or at the end of the string if the final slash is not there. 如果有一个斜线,它将在第三个斜线处停止;如果没有最后一个斜线,则将在字符串的末尾停止。

Note that you could replace .*? 请注意,您可以替换.*? with the same [^/]* expression for consistency: 使用相同的[^/]*表达式以保持一致性:

/[^/]*/([^/]*)

If characters will contain alphanumeric, then you can use the following pattern: 如果字符将包含字母数字,则可以使用以下模式:

static void Main(string[] args)
{
    string s1 = "/en/help/test/abc/def";
    string s2 = "/en/help ";
    string pattern = 
        @"(?ix)   #Options
          /       #This will match first slash
          \w+     #This will match [a-z0-9]
          /       #This will match second slash
          \w+     #Finally, this again will match [a-z0-9] until 3-rd slash (or end)";
    foreach(string s in new[] { s1, s2})
    {
        var match = Regex.Match(s, pattern);
        if (match.Success) Console.WriteLine($"Found: '{match.Value}'");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM