简体   繁体   English

正则表达式仅在两个以上的数字时用空格分隔数字

[英]Regex to separate digits with spaces only if there are more than two digits

I have a number of string that contain a mix of alphanumeric characters, for example: 我有一些包含字母数字字符混合的字符串,例如:

10
1234
123-t123b-+1
ff 8765 12

I want to separate the digits by a space, but only if there are three or more digits next to each other. 我想用空格分隔数字,但前提是彼此之间有三个或更多数字。 So the above examples would become: 因此,以上示例将变为:

10
1_2_3_4
1_2_3-t1_2_3b-+1
ff 8_7_6_5 12

(added spaces shown with underscored for clarity). (为清楚起见,添加了带下划线的空格)。 Is there a regex that can handle this or do I need to do this programatically? 是否存在可以处理此问题的正则表达式,或者我需要以编程方式进行此处理?

I think you would need something like this: 我认为您需要这样的东西:

var text = "10" + Environment.NewLine + 
           "1234" + Environment.NewLine + 
           "123-t123b-+1" + Environment.NewLine + 
           "ff 8765 12";

var regex = new Regex(@"\d{3,}");
var replacedText = regex.Replace(text, match => string.Join<char>(" ", match.Value));

not sure if C# supports \\G but try this pattern 不确定C#是否支持\\G但是尝试此模式

\B(?=\d{2})|(?<!^)\G(?=\d)

Demo 演示版


here is my second pattern without using \\G 这是我不使用\\G第二种模式

(?<=\d)(?=\d\d)|(?<=\d\d)(?=\d)

Demo 演示版
explanation: 说明:

(?<=\d)(?=\d\d)     # 0-width after 1 digit and before 2 digits ie 1^234 or 12^34  
|                   # or  
(?<=\d\d)(?=\d)     # 0-width after 2 digits and before 1 digit ie 12^34 or 123^4

i came up with this 我想出了这个

match any number followed by two numbers, the second number has to be followed by at least one number and any number digits after that 匹配任何数字后跟两个数字,第二个数字后必须至少跟一个数字,然后是任何数字

UPDATE: my pattern can be shortened to: (\\d(?=\\d{2})\\d+) 更新:我的模式可以简化为: (\\d(?=\\d{2})\\d+)

class Program
{
    static void Main(string[] args)
    {

        string pattern = @"(\d(?=\d{2})\d(?=\d)\d+)";
        string content = "10" + Environment.NewLine +
                         "1234" + Environment.NewLine +
                         "123 - t123b - +1" + Environment.NewLine +
                         "ff 8765 12";

        var reg = new Regex(pattern);
        var replacedText = reg.Replace(content, match => string.Join<char>(" ", match.Value));

        Console.WriteLine(replacedText);

        Console.ReadLine();
    }


}

Using pure regex so far I have been successful in finding position and replacing with a space except when last digit is followed by a whitespace . 到目前为止,使用纯正则表达式可以成功找到位置并用space代替,除非最后一位数字后跟whitespace Here is my solution which is close but up to the mark. 这是我的解决方案,但已接近标准。 If you wish to downvote then do provide some advice in direction of solution. 如果您希望投票,请提供一些解决方案的建议。

Regex #1 : (?<=\\d)(?=(\\d)(?=\\d|.)) 正则表达式1(?<=\\d)(?=(\\d)(?=\\d|.))

Explanation : It places the regex pointer by finding a position ( zero width ) preceded by a digit and followed by a digit. 说明 :放置正则表达式指针的方法是先找到一个位置( 零宽度 ),然后再放置一个数字和一个数字。 Latter digit ( right one ) should be followed by a digit or anything other than whitespaces . 后面的数字右边的 )后面应加上一个数字或除whitespaces任何其他数字。

Regex101 Demo#1 Regex101演示#1


Regex #2 : (?<=\\d)(?=(\\d|\\w)(?=\\d|.)) 正则表达式2(?<=\\d)(?=(\\d|\\w)(?=\\d|.))

Explanation : This one has a little variation. 说明 :这个有一些变化。 It places the regex pointer by finding a position ( zero width ) preceded by a digit and followed by a digit or anything other than whitespaces . 它通过找到一个位置( 零宽度 )来放置正则表达式指针 ,位置前面有一个数字,然后是一个数字或除whitespaces以外的任何其他whitespaces

Regex101 Demo #2 Regex101演示#2

Now the only problem left is matching the whitespaces followed by a valid case. 现在剩下的唯一问题是匹配whitespaces和有效案例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM