[英]Regular expression with danish phone number
我正在嘗試編寫正則表達式,以便可以用鏈接替換以下電話號碼:
first
+45 55 44 33 22
second
+4555443322
third
+45 55443322
fourth
55443322
and last
55 44 33 22
currentley我的正則表達式如下所示:
Regex phoneRegex = new Regex(@"[0-9]{8}", RegexOptions.IgnoreCase);
Regex phoneRegexInternation = new Regex(@"(?:\+\[0-9]{10})", RegexOptions.IgnoreCase);
Regex phoneRegexwithSpace = new Regex(@"[0-9]{2}\s*[0-9]{2}\s*[0-9]{2}\s*[0-9]{2}", RegexOptions.IgnoreCase);
Regex phoneRegexInternationWithSpaceAfterAreaCode = new Regex(@"(\+?[0-9]{2})\s*[0-9]{8}", RegexOptions.IgnoreCase);
Regex phoneRegexwithSpaceInternational = new Regex(@"(\+?[0-9]{2})\s*[0-9]{2}\s*[0-9]{2}\s*[0-9]{2}\s*[0-9]{2}", RegexOptions.IgnoreCase);
但我的結果是我
first +Acceptere opgaven for at se telefonnummer. 22 second +Acceptere opgaven for at se telefonnummer.22 third +45 Acceptere opgaven for at se telefonnummer. fourth Acceptere opgaven for at se telefonnummer. and last Acceptere opgaven for at se telefonnummer.
“用於電話會議的接受者opgaven”是丹麥語,表示接受分配以查看電話號碼,它是一個鏈接,但是正如您所知,它沒有隱藏加號和一些正確的數字。
任何可以幫助的人嗎?
var input = @"first
+45 55 44 33 22
second
+4555443322
third
+45 55443322
fourth
55443322
and last
55 44 33 22";
var regex = new Regex(@"(?<inter>\+45\s?)?(?<part1>\d{2})\s?(?<part2>\d{2})\s?(?<part2>\d{2})\s?(?<part4>\d{2})");
var matches = regex.Matches(input);
var output = new StringBuilder();
var lastindex = 0;
var index = 0;
foreach (Match m in matches){
index = m.Index;
output.Append(input.Substring(lastindex, index - lastindex));
var phone = $"+45{m.Groups["part1"]}{m.Groups["part2"]}{m.Groups["part3"]}{m.Groups["part4"]}";
output.Append($"<a href=\"#\" data-original=\"{m.Value}\" data-generic=\"{phone}\">Acceptere opgaven for at se telefonnummer</a>");
lastindex = m.Index + m.Length;
}
Console.WriteLine(output.ToString());
結果是:
first
<a href="#" data-original="+45 55 44 33 22" data-generic="+45553322">Acceptere opgaven for at se telefonnummer</a>
second
<a href="#" data-original="+4555443322" data-generic="+45553322">Acceptere opgaven for at se telefonnummer</a>
third
<a href="#" data-original="+45 55443322" data-generic="+45553322">Acceptere opgaven for at se telefonnummer</a>
fourth
<a href="#" data-original="55443322" data-generic="+45553322">Acceptere opgaven for at se telefonnummer</a>
and last
<a href="#" data-original="55 44 33 22" data-generic="+45553322">Acceptere opgaven for at se telefonnummer</a>
如果不了解如何應用這些正則表達式,就很難確定您在做什么。 確切了解您期望看到的內容也將有所幫助。 就是說,我會盡力回答。
基於正則表達式,輸入和輸出,似乎您可能無意中將錯誤的正則表達式應用於錯誤的輸入。 如前所述,在不確切了解您如何使用它們的情況下,我們無法知道。 請注意,phoneRegexWithSpace將匹配第一個輸入,但+和22除外,它們在您的輸出中都會彈出。
要更正它,您可以做一些事情:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.