gpt4 book ai didi

c# - 正则表达式 11 位字符串捕获

转载 作者:行者123 更新时间:2023-11-30 13:40:06 24 4
gpt4 key购买 nike

String pattern = @"^(\d{11})$";
String input = "You number is:11126564312 and 12234322121 \n\n23211212345";
Match match = Regex.Match(input,pattern);

从上面的代码中,我计划捕获上面文本中存在的 11 位数字字符串,但是 match.Success 总是返回 false。任何想法。

最佳答案

这是因为你使用了^$

解释:您的正则表达式的含义是“匹配从头到尾恰好包含 11 位数字的任何字符串”。字符串 You number is:11126564312 and 12234322121\n\n23211212345 不是这样的字符串。 01234567890 就像那个字符串。

您需要什么:您需要正则表达式来匹配任何恰好包含 11 位数字的字符串从头到尾 被省略。 ^$ 用于此。所以你需要这个正则表达式。

String pattern = @"(\d{11})";

由于要捕获的子模式包含整个正则表达式,因此您根本不需要 ()。只是正则表达式不行。

String pattern = @"\d{11}";

关于c# - 正则表达式 11 位字符串捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8961864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com