使用REGEX具有可变长度零的填充号 [英] Pad number with variable length zeroes using REGEX

查看:108
本文介绍了使用REGEX具有可变长度零的填充号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,客户输入了一个数字,我需要通过在数字前面填充一定数量的零来验证该数字.我验证的字段的最大长度为9位数字,因此,如果客户输入"123",则需要在其前面填充6个零并进行验证.听起来很容易,对吧?

I have a situation where client inputs a number, and I need to validate that number by padding a certain number of zeroes in front of it. The max length of the field that I validate is 9 digits, so if client enters '123', I need to pad it with 6 zeros in front of it and validate. Sounds easy enough, right?

好吧,这是关键,我对过程的任何部分都没有代码控制.对我而言,唯一可用的东西是2个属性"-一个正则表达式模式和一个正则表达式替换,这是现有代码用于处理输入的内容.

Well, here is the kicker, I have no code-control over any part of the process. The only things available to me are 2 'properties' - a regex pattern and a regex replace that already existing code uses to process input.

那么,是否可以使用带有正则表达式替换的正则表达式模式填充输入?同样,我无权访问该代码,因此我无法应用诸如向左填充零的解决方案

So, is it possible to pad an input using a regex pattern with a regex replace? Again, I have no access to the code, so I cannot apply solution such as this Pad left with zeroes

我所能做的就是定义一个已经存在的代码正在使用的搜索模式和一个替换模式.

What I can do is define a search pattern and a replace pattern that already existing code is using.

这是我要找的东西:

INPUT       OUTPUT
1           000000001
12          000000012
123         000000123
12345678    012345678
123456789   123456789

目前我所拥有的模式是:

Currently what I have for pattern is:

([0-9]{1,9})

并替换:

000000000$1

但是它不能完成我需要的工作,它在输入的前面添加了固定数量的零,而不是可变数.考虑到我的工作限制,我怀疑该任务是不可能的,但我还是想问问.

But it doesn't do the job I need, it adds a fixed number of zeroes in front of the input, not a variable number. I suspect the task is impossible, given the restrictions I am working under, but I thought id ask anyway.

PS:读取正则表达式的代码的语言是.NET(C#)

PS: the language of the code that reads regex is .NET (C#)

重申一下,我无法以任何方式更改/更改代码,甚至看不到它.我认为这是现有代码的作用:

EDIT 1: Just to reiterate, I cannot change/alter code in any way, I cannot even see it. I assume this is what the existing code does:

  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);

patternreplacement是从配置文件中读取的字符串,我可以更改这两个字符串的值.而已.我不能更改读取它的代码,也不能循环它两次进行正则表达式.

pattern and replacement are strings being read from a configuration file, and I can change the values of those 2 strings. That's it. I cannot alter code reading it nor can I loop it to regex it twice or anything.

并不是我要找的东西,而是(VB.NET代码):

EDIT 2: Not exactly what I'm looking for but (VB.NET code):

Dim result As String = Regex.Replace(input, pattern, Function(match As Match)
                                                         Dim v As String = match.ToString()
                                                         Return String.Format("{0:000000000}", Val(v))
                                                     End Function)

问题是,Function是代码,但是如果我将其粘贴到属性中,它将被读取为字符串,并且不会作为代码执行,而是作为替换字符串执行.如果我只能弄清楚如何将该字符串视为代码,那将解决我的问题.

Problem is, the Function is code, but if I stick that into property it will be read as a string and will not execute as code but as a replacement string. If I could only figure out how to make that string be treated as code, that would solve my issue.

来自EDIT 2的优化代码(这次是在C#中):

EDIT 3: optimized code from EDIT 2(this time in C#):

var result = Regex.Replace(input, pattern, (v) => string.Format("{0:000000000}", double.Parse(v.Value)));

在不更改服务器上的代码(我无权访问)的情况下,仍然无法弄清楚如何执行这段代码(v) => string.Format("{0:000000000}", double.Parse(v.Value)),因为它存储为字符串.如果没有访问服务器上的更改代码的感觉,这似乎是不可能的.

Still cannot figure out how to execute this piece of code (v) => string.Format("{0:000000000}", double.Parse(v.Value)) without changing code on the server (which I have no access to), because it is stored as string. It feels like impossible to do without access to change code on the server.

看一下执行正则表达式的服务器代码:

EDIT 4: Got a look at the server code that executes the regex:

searchVal = Regex.Replace(accountNumber, pattern, replace);

无法更改代码"限制仍然适用,因此我以前的编辑建议仍然无法实现,因为它被视为字符串而不是代码.考虑到我的工作限制,我认为这是不可能完成的任务.

The "cannot change code" restriction still applies, so my suggestion from previous edit remains unreachable because it is treated as string and not code. I think this is an impossible task, given the restrictions i am working under.

感谢所有尝试提供帮助的人

Thank you all who tried to help

推荐答案

正如您所说,.PadLeft()确实是解决此问题的方法,但是您提到不能使用它.不幸的是,C#不支持有条件替换正则表达式.我认为对您来说最简单的选择是创建8个不同的正则表达式,例如:

As you said, .PadLeft() really is the way to go about this, but you mention that you can't use it. Unfortunately, C# doesn't support conditional replacements for regex. I think the easiest option for you would be to create 8 different regular expressions as such:

\b([1-9])\b            00000000$1
\b([1-9]\d)\b          0000000$1
\b([1-9]\d{2})\b       000000$1
\b([1-9]\d{3})\b       00000$1
\b([1-9]\d{4})\b       0000$1
\b([1-9]\d{5})\b       000$1
\b([1-9]\d{6})\b       00$1
\b([1-9]\d{7})\b       0$1


或者,可以在一个正则表达式中完成所有这些操作,但是您需要在输入中附加一个 dictionary ,如下所示:


Alternatively, doing all of this in one regex is possible, but you'd need to append a dictionary to the input as the following suggests:

附加以下内容(请注意,SO显示空格,但数字由制表符分隔):

Appending the following (note that SO shows spaces, but the numbers are separated by tabs):

1   00000000
2   0000000
3   000000
4   00000
5   0000
6   000
7   00
8   0

替换:${x}$1

如何添加字典?好吧,如果您能够将其附加到最简单的输入中.否则,可以使用正则表达式使用\z .然后,您必须运行上面的正则表达式才能使用字典.

How to add the dictionary? Well, if you have the ability to append it to the input that's the easiest option. Otherwise, using regex you can use \z as seen here. You'd then have to run the regex above to use the dictionary.

替换:\n1\t00000000\n2\t0000000\n3\t000000\n4\t00000\n5\t0000\n6\t000\n7\t00\n8\t0

如果您无法在单个输入上运行多个正则表达式,恐怕您要完成的工作无法完成,因为每种方法至少需要对输入运行2次才能提供预期的输出.

If you're unable to run multiple regular expressions on a single input, I'm afraid what you're trying to accomplish cannot be done as each method requires at least 2 runs over the input to give you the expected output.

这篇关于使用REGEX具有可变长度零的填充号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆