Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用正则表达式限制字符数_C#_Regex - Fatal编程技术网

C# 使用正则表达式限制字符数

C# 使用正则表达式限制字符数,c#,regex,C#,Regex,我使用以下正则表达式将字符串中的所有URL转换为完整的超链接: var r = new Regex("(https?://[^ ]+)"); return r.Replace(Action, "<a target=\"_blank\" href=\"$1\">$1</a>"); var r=newregex(“(https?:/[^]+)”; 返回r.Replace(操作“”); 我想限制标签中显示的字符数,如果可能的话,如果超过长度,可以添加省略号 e、 g 我尝试

我使用以下正则表达式将字符串中的所有URL转换为完整的超链接:

var r = new Regex("(https?://[^ ]+)");
return r.Replace(Action, "<a target=\"_blank\" href=\"$1\">$1</a>");
var r=newregex(“(https?:/[^]+)”;
返回r.Replace(操作“”);
我想限制标签中显示的字符数,如果可能的话,如果超过长度,可以添加省略号

e、 g


我尝试过使用lookarounds(未成功)来实现这一点,我想知道是否有人有更好的解决方案?

下面的正则表达式将为您提供您想要的

((https?://[^ ]){20}[^ ]+)
这将创建两个捕获组

  • 捕获整个URL
  • 捕获特定长度的URL(在本例中为20)
  • 所需的只是添加截断

    Regex.Replace(Action, "((https?://[^ ]){20}[^ ]+)", "<a target=\"_blank\" href=\"$1\">$2...</a>"));
    
    var match = Regex.Match(Action, "(https?://[^ ]{50})?[^ ]+");
    // if the display part group has matched something, we need to truncate
    var displayText = match.Groups[1].Length > 0 ? String.Format("{0}...", match.Groups[1]) : match.ToString();
    Console.WriteLine(String.Format("<a target=\"_blank\" href=\"{0}\">{1}</a>", match, displayText));
    

    我已经更新了

    我认为用简单的正则表达式替换是不可能的,但幸运的是.NET允许您执行更复杂的替换。首先,我们设置正则表达式以捕获一个组中URL开始后的第一个(例如)25个字符,以及第二个可选组中的任何其他字符:

    var r = new Regex("(https?://[^ ]{1,25})([^ ]+)?");
    
    如果URL开始后少于25个字符,则第二个组将完全失败,但它是可选的,因此不会使正则表达式作为一个整体失败

    然后,在替换时,我们在决定是否添加点时检查第二组是否匹配:

    var s = r.Replace(
        Action,
        m => string.Concat(
            "<a target=\"_blank\" href=\"",
            m.Value,
            "\">",
            m.Groups[1].Value,
            (m.Groups[2].Success ? "..." : ""),
            "</a>"));
    
    我得到输出

    hello <a target="_blank" href="http://www.google.com">http://www.google.com</a>
    world <a target="_blank"
        href="http://www.loooooooooooooooooooooooooooooooooooooooooong.com">
        http://www.loooooooooooooooooooo...</a> !
    
    你好 世界!
    这最好由一个自定义程序来解决,使用一个函数进行替换

    string Action = "Somebody turn http://stackoverflow.com/questions/20494457/limiting-the-number-of-characters-using-regular-expression into a link please.";
    var r = new Regex("(https?://\\S+)");
    return r.Replace(Action,
        match => {
            string display = match.Value;
            if (display.Length > 30)
            {
                display = display.Substring(0, 30) + "...";
            }
            return "<a target=\"_blank\" href=\"" + match.Value + "\">" + display + "</a>";
        });
    
    string Action=“某人转身http://stackoverflow.com/questions/20494457/limiting-the-number-of-characters-using-regular-expression 请输入链接。“;
    var r=新的正则表达式(“(https?:/\\S+”);
    返回r.更换(动作,
    匹配=>{
    字符串显示=匹配.Value;
    如果(显示长度>30)
    {
    display=display.Substring(0,30)+“…”;
    }
    返回“”;
    });
    
    返回:

    请有人变成链接。
    
    不可能,至少不能使用省略号。另外,你应该使用。(解决这一问题的一种方法是使用CSS(
    a{display:inline block;width:20em;overflow:hidden;text overflow:省略号;}
    ),因此字符限制仅用于显示目的?(实际URL将保持完整)。你不需要正则表达式就可以做到这一点,只需将完整链接加上子字符串并附加一些点即可…@musefan你如何使用子字符串来匹配任何URL?@Rawling:你不需要…你只需要一行函数,即使链接很短,你也会得到“…”。@Rawling啊,是的,你会这样做。将进行更新以适应这一点。尽管如此,如果你要ng要继续使用
    MatchEvaluator
    路径,您最好完全删除组,只需
    substring
    匹配即可。令人失望的是,正则表达式确实支持有条件的“是否该组匹配”逻辑,但仅在匹配时才支持,而不是在替换时。根据我的口味,这是最好的答案
    string Action = "Somebody turn http://stackoverflow.com/questions/20494457/limiting-the-number-of-characters-using-regular-expression into a link please.";
    var r = new Regex("(https?://\\S+)");
    return r.Replace(Action,
        match => {
            string display = match.Value;
            if (display.Length > 30)
            {
                display = display.Substring(0, 30) + "...";
            }
            return "<a target=\"_blank\" href=\"" + match.Value + "\">" + display + "</a>";
        });