使用 C# 动态正则表达式表示数字范围
我正在查看英国邮政编码,并尝试弄清楚如何从数据库 (英国邮政编码的第一部分)
获取数据并使用 c# 为其动态创建正则表达式。例如:
AB44-56
我知道我想要什么作为输出:
AB([4][4-9]|[5][0-6])+
但是,我无法弄清楚如何用逻辑来做到这一点,也许我需要先将字母与数字分开,但我不能这样做使用拆分。
我也有其他组合 - 单一范围:
AB31 would be AB[3][1]+
有些只有字母:
BT would be BT+
有些只有一个字母和 1 或两个数字:
G83 Would be G[8][3]
任何建议或指导将非常适合如何编码。
I'm looking at UK postcodes and trying to work out how I can take data from a database (the first part of a UK postcode)
and dynamically create a regexp for them using c#. For example:
AB44-56
I know what I want as an output:
AB([4][4-9]|[5][0-6])+
However, I can't work out how I might be able to do this with logic, perhaps I need to split the Letters from the numbers first, but i can't do that using split.
I have other combinations too - single range:
AB31 would be AB[3][1]+
Some with just letters:
BT would be BT+
Some with a single letter and 1 or two numbers:
G83 Would be G[8][3]
Any suggestions or guidance would be very much appriciated how this may be coded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
a来自维基百科 英国邮政编码:
所以
可能有效。
编辑:此外,如果您试图限制邮政编码以表示那些与数据库中的前缀相同的邮政编码,您可以这样做。
afrom wikipedia UK postal codes :
so
might work.
EDIT: Also if you are trying to restric the postal codes to say those with the same prefix as the ones in the database you could do this.