简体   繁体   English

C#正则表达式拆分为非零数字,后跟任意数字零

[英]C# regex split for A non-zero digit followed by an arbitrary number of zero

I have a number as a string "10009003" i would like to split it using regex to always get the first non-zero digit and include all trailing zeroes. 我有一个数字作为字符串“ 10009003”,我想使用正则表达式将其拆分,以始终获取第一个非零数字并包括所有尾随零。 Then get the next non-zero number followed by its trailing zeroes. 然后获取下一个非零数字,其后跟零。 If a non-zero number has no trailing zeroes just grab it. 如果非零数字没有尾随零,则抓住它。

The above number string should result in array/list of length/count 3 as follows [0] = 1000, [1] = 900, [2] =3 上面的数字字符串应导致长度为3的数组/列表,如下所示:[0] = 1000,[1] = 900,[2] = 3

I have tried this code; 我已经试过了这段代码;

var someNumberString = "10009003";     
string s = @"[^1-9]+";
string[] strArray = Regex.Split(someNumberString, s);

which only output the following but the zeros are missing; 仅输出以下内容,但不包含零; [0] = 1 , [1] = 9 , [2] = 3 [0] = 1,[1] = 9,[2] = 3

How do i get the regex to include the trailing zeros? 我如何使正则表达式包含尾随零?

Thanks in advance for the help:) 先谢谢您的帮助:)

You can use this lookbehind and lookahead based regex: 您可以使用基于lookbehind和lookahead的正则表达式:

var someNumberString = "10009003";     
string s = @"(?<=[1-9]0*)(?=[1-9])";
string[] strArray = Regex.Split(someNumberString, s);

Console.Write(string.Join(" ", strArray));
//=> 1000 900 3

Online Demo 在线演示

Explanation: 说明:

  • (?<=[1-9]0*) ( Lookbehind ) - if current position preceded by a non-zero digit and 0 or more zeroes (?<=[1-9]0*)向后看 )-如果当前位置前面有一个非零数字和0个或多个零
  • (?=[1-9]) ( Lookahead ) - if current position is followed by a non-zero digit (?=[1-9])前瞻 )-如果当前位置后跟一个非零数字

You could use regex.Matches function like below. 您可以使用regex.Matches函数,如下所示。

String input = @"10009003";
Regex rgx = new Regex(@"[1-9]+0*");
foreach (Match m in rgx.Matches(input))
Console.WriteLine(m.Groups[0].Value);

[1-9] matches the digit which ranges from 1 to 9. 0* matches zero or more 0 's. [1-9]匹配范围从1到9的数字0*匹配零个或多个0

IDEONE IDEONE

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

相关问题 正则表达式:非零数字,后跟一个或多个空格,后跟非零数字 - Regex: non-zero number followed by one or more spaces followed by non-zero number C# - 获取数组最后一个非零数的索引 - C# - Get the Index of the last non-zero number of an array 为什么 C# 允许在浮点类型中将非零数除以零? - Why does C# allow dividing a non-zero number by zero in floating-point type? 创建给定C#中的Type的任何数字/原始值类型的非零实例 - Create a non-zero instance of any number/primitive value type given a Type in C# 检查 C# BitArray 非零值的最快方法 - Fastest way to check C# BitArray for non-zero value 向左拉数组中的所有非零元素-C# - pull all non-zero elements in an array to the left - c# 在我的OpenCL / Cloo(C#)程序中,“零复制”比非零复制慢 - “Zero copy” is slower in my OpenCL/Cloo(C#) program than non-zero copy C#RegEx为十进制数字或零(0) - C# RegEx for decimal number or zero (0) 可以匹配具有任意小数位的非零浮点数的最短正则表达式是什么? - What's the shortest regex that can match non-zero floating point numbers with any number of decimal places? 获取异常“非零金额必须四舍五入为整数”Sage 50 C# WPF? - Getting exception 'Non-zero amount must be rounded to whole currency' Sage 50 C# WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM