gpt4 book ai didi

c# - 某些文化中的正则表达式和资本 I

转载 作者:IT王子 更新时间:2023-10-29 04:25:01 28 4
gpt4 key购买 nike

在某些文化中大写“I”有什么问题?我发现在某些文化中,在特殊条件下无法找到 - 如果您正在寻找带有标志 RegexOptions.IgnoreCase 的 [a-z]。这是示例代码:

var allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
var allLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var allLettersCount = allLetters.Length;

foreach (var culture in allCultures)
{
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

var matched = string.Empty;
foreach (var m in Regex.Matches(allLetters, "[A-Za-z0-9]", RegexOptions.IgnoreCase))
matched += m;

var count = matched.Length;
if (count != allLettersCount)
Console.WriteLine("Culture '{0}' - {1} missing; Matched: {2}", culture.Name, (allLettersCount - count).ToString(), matched);
}

输出是(注意每一行都缺少大写字母 I):

Culture 'az' - 1 missing; Matched:          ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'az-Cyrl' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'az-Cyrl-AZ' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'az-Latn' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'az-Latn-AZ' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'tr' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Culture 'tr-TR' - 1 missing; Matched: ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

有趣的是,如果不使用标志“IgnoreCase”,那么它工作正常,并找到“I”。

最佳答案

答案在Wikipedia中:

The casing of the dotless and dotted I forms differ from other languages. That implies that a case insensitive matching expected by an English person doesn't match the expectations of a Turkish user. The "Turkish I" is often used as an example of the problems with case insensitivity in computing.

另一种解释可以在 MSDN 上找到:

enter image description here

关于c# - 某些文化中的正则表达式和资本 I,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29674188/

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