需要帮助使用正则表达式 function/trimming 我的字符串
Need help using regex function/trimming my string
我在这上面停留了一段时间,我的输出如下:前 2 个字母表示走廊编号,因此第一个为 17,后面的数字表示货架编号,(位置在门厅)。正如您在 17 号走廊 1 号货架上看到的那样,我们有 A1 或 A,但这并不重要。我希望 171A1 的输出为 171,而 15211 的输出为 1521,所以我想删除末尾的字母以及后面可能跟的数字。
171A1
171A1
171A
171A0
15211
15211
15211
15210
15190
我尝试使用 string.Remove(string.Length-2) 但这不起作用,因为我们有 171A,例如,它应该变成 171。任何帮助将不胜感激。
如果您不需要它是 REGEX,这应该可以工作
var rawString = "15211";
var maxLength = 4;
var trimmedResult = new string(
rawString.TakeWhile(char.IsNumber) // loop through characters using LINQ. since your string starts with numbers, you can just keep taking until you bump into a character
.Take(maxLength) // limit the length for cases like "15211" where you don't need the extra numbers in the end
.ToArray() // so we can use the new string() constructor
);
对于问题中的部分,您可以使用带有捕获组的模式,并在替换中使用该组:
^([0-9]{2,}?)[A-Z]?[0-9]?$
^
字符串开头
([0-9]{2,}?)
捕获第1组,匹配2位0-9非贪心
[A-Z]?[0-9]?
匹配可选字符 A-Z 和可选数字 0-9
$
字符串结束
看到一个regex demo。
string pattern = @"^([0-9]{2,}?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
Console.WriteLine(Regex.Replace(s, pattern, ""));
}
输出
171
171
171
171
1521
1521
1521
1521
1519
如果要在 2 位数字后用点分隔输出,可以使用 2 个捕获组并匹配第二组中的至少 1 个或多个数字:
string pattern = @"^([0-9]{2})([0-9]+?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
Console.WriteLine(Regex.Replace(s, pattern, "."));
}
输出
17.1
17.1
17.1
17.1
15.21
15.21
15.21
15.21
15.19
看到一个C# demo
我在这上面停留了一段时间,我的输出如下:前 2 个字母表示走廊编号,因此第一个为 17,后面的数字表示货架编号,(位置在门厅)。正如您在 17 号走廊 1 号货架上看到的那样,我们有 A1 或 A,但这并不重要。我希望 171A1 的输出为 171,而 15211 的输出为 1521,所以我想删除末尾的字母以及后面可能跟的数字。
171A1
171A1
171A
171A0
15211
15211
15211
15210
15190
我尝试使用 string.Remove(string.Length-2) 但这不起作用,因为我们有 171A,例如,它应该变成 171。任何帮助将不胜感激。
如果您不需要它是 REGEX,这应该可以工作
var rawString = "15211";
var maxLength = 4;
var trimmedResult = new string(
rawString.TakeWhile(char.IsNumber) // loop through characters using LINQ. since your string starts with numbers, you can just keep taking until you bump into a character
.Take(maxLength) // limit the length for cases like "15211" where you don't need the extra numbers in the end
.ToArray() // so we can use the new string() constructor
);
对于问题中的部分,您可以使用带有捕获组的模式,并在替换中使用该组:
^([0-9]{2,}?)[A-Z]?[0-9]?$
^
字符串开头([0-9]{2,}?)
捕获第1组,匹配2位0-9非贪心[A-Z]?[0-9]?
匹配可选字符 A-Z 和可选数字 0-9$
字符串结束
看到一个regex demo。
string pattern = @"^([0-9]{2,}?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
Console.WriteLine(Regex.Replace(s, pattern, ""));
}
输出
171
171
171
171
1521
1521
1521
1521
1519
如果要在 2 位数字后用点分隔输出,可以使用 2 个捕获组并匹配第二组中的至少 1 个或多个数字:
string pattern = @"^([0-9]{2})([0-9]+?)[A-Z]?[0-9]?$";
string[] strings = { "171A1", "171A1", "171A", "171A0", "15211", "15211", "15211", "15210", "15190" };
foreach (String s in strings)
{
Console.WriteLine(Regex.Replace(s, pattern, "."));
}
输出
17.1
17.1
17.1
17.1
15.21
15.21
15.21
15.21
15.19
看到一个C# demo