Regexp查找并替换找到的值

时间:2022-04-03 23:15:41

I have UK postcodes data and I would like to sort them alphabeticaly, when I do that the result is as follows;

我有英国的邮政编码数据,我想按字母顺序对它们进行排序,当我这样做时,结果如下;

N10-XX
N1-XX
N2-XX
N3-XX
N4-XX
N5-XX

What I want is that as follows;

我想要的是如下;

N1-XX
N2-XX
N3-XX
N4-XX
N5-XX
N10-XX

Basicaly I need to add 0 at the begining of the number if it is 1 digit. like N1 should be N01 to be able to do that, what is the regexp pattern for that?

基本上我需要在数字的开头添加0,如果它是1位数。像N1应该是N01才能做到这一点,那是什么样的正则表达式?

Many thanks.

非常感谢。

3 个解决方案

#1


2  

Well if you are bent on using Regex, then this should do it

好吧,如果你一直在使用正则表达式,那么这应该这样做

var text = @"N10-XX
N1-XX
N2-XX
N3-XX
N4-XX
N5-XX";

text = Regex.Replace(text, @"^N(\d)-", "N0$1-", RegexOptions.Multiline);

that said you obviously will be altering the original data, so I am not sure if this is even applicable

那说你显然会改变原始数据,所以我不确定这是否适用

If you want to sort numerically, but preserve the original data, then you may need to do something like this

如果要以数字方式排序,但保留原始数据,则可能需要执行此类操作

text.Split('\n')
    .Select(o => new { Original = o, Normal = Regex.Replace(o, @"^N(\d)-", "N0$1-", RegexOptions.Compiled)})
    .OrderBy(o => o.Normal)
    .Select(o => o.Original)

#2


1  

I'm not sure from the example which numbers in the post code need to be ordered. here is some regex examples for valid uk post codes http://blogs.creative-jar.com/post/Valid-UK-Postcdoe-formats.aspx. if you incorporate this using the method above you should be able to do it.

我不确定从示例中需要订购邮政编码中的哪些数字。以下是有效的英国邮政编码http://blogs.creative-jar.com/post/Valid-UK-Postcdoe-formats.aspx的一些正则表达式示例。如果你使用上面的方法合并它,你应该能够做到这一点。

#3


0  

Here is a sort function returning original string in natural(?) order.

这是一个以自然(?)顺序返回原始字符串的sort函数。

List<string> list1 = new List<string>{ "N10-XX","N1-XX","N2-XX","N3-XX","N4-XX","N5-XX" };
List<string> list2 = new List<string>() { "File (5).txt", "File (1).txt", "File (10).txt", "File (100).txt", "File (2).txt" };   

var sortedList1 = MySort(list1).ToArray();
var sortedList2 = MySort(list2).ToArray();


public static IEnumerable<string> MySort(IEnumerable<string> list)
{
    int maxLen = list.Select(s => s.Length).Max();
    Func<string, char> PaddingChar = s => char.IsDigit(s[0]) ? ' ' : char.MaxValue;

    return 
        list.Select(s =>
                new
                {
                    OrgStr = s,
                    SortStr = Regex.Replace(s, @"(\d+)|(\D+)", m => m.Value.PadLeft(maxLen, PaddingChar(m.Value)))
                })
            .OrderBy(x => x.SortStr)
            .Select(x => x.OrgStr);
}

#1


2  

Well if you are bent on using Regex, then this should do it

好吧,如果你一直在使用正则表达式,那么这应该这样做

var text = @"N10-XX
N1-XX
N2-XX
N3-XX
N4-XX
N5-XX";

text = Regex.Replace(text, @"^N(\d)-", "N0$1-", RegexOptions.Multiline);

that said you obviously will be altering the original data, so I am not sure if this is even applicable

那说你显然会改变原始数据,所以我不确定这是否适用

If you want to sort numerically, but preserve the original data, then you may need to do something like this

如果要以数字方式排序,但保留原始数据,则可能需要执行此类操作

text.Split('\n')
    .Select(o => new { Original = o, Normal = Regex.Replace(o, @"^N(\d)-", "N0$1-", RegexOptions.Compiled)})
    .OrderBy(o => o.Normal)
    .Select(o => o.Original)

#2


1  

I'm not sure from the example which numbers in the post code need to be ordered. here is some regex examples for valid uk post codes http://blogs.creative-jar.com/post/Valid-UK-Postcdoe-formats.aspx. if you incorporate this using the method above you should be able to do it.

我不确定从示例中需要订购邮政编码中的哪些数字。以下是有效的英国邮政编码http://blogs.creative-jar.com/post/Valid-UK-Postcdoe-formats.aspx的一些正则表达式示例。如果你使用上面的方法合并它,你应该能够做到这一点。

#3


0  

Here is a sort function returning original string in natural(?) order.

这是一个以自然(?)顺序返回原始字符串的sort函数。

List<string> list1 = new List<string>{ "N10-XX","N1-XX","N2-XX","N3-XX","N4-XX","N5-XX" };
List<string> list2 = new List<string>() { "File (5).txt", "File (1).txt", "File (10).txt", "File (100).txt", "File (2).txt" };   

var sortedList1 = MySort(list1).ToArray();
var sortedList2 = MySort(list2).ToArray();


public static IEnumerable<string> MySort(IEnumerable<string> list)
{
    int maxLen = list.Select(s => s.Length).Max();
    Func<string, char> PaddingChar = s => char.IsDigit(s[0]) ? ' ' : char.MaxValue;

    return 
        list.Select(s =>
                new
                {
                    OrgStr = s,
                    SortStr = Regex.Replace(s, @"(\d+)|(\D+)", m => m.Value.PadLeft(maxLen, PaddingChar(m.Value)))
                })
            .OrderBy(x => x.SortStr)
            .Select(x => x.OrgStr);
}