gpt4 book ai didi

RegEx:英国固定电话、手机号码

转载 作者:行者123 更新时间:2023-12-01 08:19:01 26 4
gpt4 key购买 nike

我一直在努力寻找合适的解决方案:-

我需要一个匹配所有英国电话号码和手机的正则表达式。

到目前为止,这似乎涵盖了英国的大部分数字:

^0\d{2,4}[ -]{1}[\d]{3}[\d -]{1}[\d -]{1}[\d]{1,4}$

但是,手机号码不适用于此正则表达式或写在单个实心块中的电话号码,例如 01234567890。

谁能帮我创建所需的正则表达式?

最佳答案

这个正则表达式可以吗?

//  using System.Text.RegularExpressions;

/// <summary>
/// Regular expression built for C# on: Wed, Sep 8, 2010, 06:38:28
/// Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///
/// A description of the regular expression:
///
/// [1]: A numbered capture group. [\+44], zero or one repetitions
/// \+44
/// Literal +
/// 44
/// [2]: A numbered capture group. [\s+], zero or one repetitions
/// Whitespace, one or more repetitions
/// [3]: A numbered capture group. [\(?]
/// Literal (, zero or one repetitions
/// [area_code]: A named capture group. [(\d{1,5}|\d{4}\s+?\d{1,2})]
/// [4]: A numbered capture group. [\d{1,5}|\d{4}\s+?\d{1,2}]
/// Select from 2 alternatives
/// Any digit, between 1 and 5 repetitions
/// \d{4}\s+?\d{1,2}
/// Any digit, exactly 4 repetitions
/// Whitespace, one or more repetitions, as few as possible
/// Any digit, between 1 and 2 repetitions
/// [5]: A numbered capture group. [\)?]
/// Literal ), zero or one repetitions
/// [6]: A numbered capture group. [\s+|-], zero or one repetitions
/// Select from 2 alternatives
/// Whitespace, one or more repetitions
/// -
/// [tel_no]: A named capture group. [(\d{1,4}(\s+|-)?\d{1,4}|(\d{6}))]
/// [7]: A numbered capture group. [\d{1,4}(\s+|-)?\d{1,4}|(\d{6})]
/// Select from 2 alternatives
/// \d{1,4}(\s+|-)?\d{1,4}
/// Any digit, between 1 and 4 repetitions
/// [8]: A numbered capture group. [\s+|-], zero or one repetitions
/// Select from 2 alternatives
/// Whitespace, one or more repetitions
/// -
/// Any digit, between 1 and 4 repetitions
/// [9]: A numbered capture group. [\d{6}]
/// Any digit, exactly 6 repetitions
///
///
/// </summary>
public Regex MyRegex = new Regex(
"(\\+44)?\r\n(\\s+)?\r\n(\\(?)\r\n(?<area_code>(\\d{1,5}|\\d{4}\\s+"+
"?\\d{1,2}))(\\)?)\r\n(\\s+|-)?\r\n(?<tel_no>\r\n(\\d{1,4}\r\n(\\s+|-"+
")?\\d{1,4}\r\n|(\\d{6})\r\n))",
RegexOptions.IgnoreCase
| RegexOptions.Singleline
| RegexOptions.ExplicitCapture
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);



//// Replace the matched text in the InputText using the replacement pattern
// string result = MyRegex.Replace(InputText,MyRegexReplace);

//// Split the InputText wherever the regex matches
// string[] results = MyRegex.Split(InputText);

//// Capture the first Match, if any, in the InputText
// Match m = MyRegex.Match(InputText);

//// Capture all Matches in the InputText
// MatchCollection ms = MyRegex.Matches(InputText);

//// Test to see if there is a match in the InputText
// bool IsMatch = MyRegex.IsMatch(InputText);

//// Get the names of all the named and numbered capture groups
// string[] GroupNames = MyRegex.GetGroupNames();

//// Get the numbers of all the named and numbered capture groups
// int[] GroupNumbers = MyRegex.GetGroupNumbers();

请注意空格和破折号是可选的,并且可以成为其中的一部分。此外,它现在分为两个捕获组,称为 area_codetel_no 以将其分解并更容易提取。

关于RegEx:英国固定电话、手机号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3669516/

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