你如何制作正则表达式只有在使用逗号时才会失败 [英] How can you make a regex there will only fail if comma is used

查看:62
本文介绍了你如何制作正则表达式只有在使用逗号时才会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



谁能告诉我如何为这个问题制作正则表达式。我有一个货币问题,有时人们错误地键入'。'而不是','然而,使用'。'来分隔数字应该没问题



1,0

1.0< - 失败

1,92

1.92< - 失败

100,2

100.2< - 失败

100,02

100.02< - 失败

1.000 ,00

1.000.00< - 失败

1.000.000,01

1.000.000.02< - fail



计划是将它用作aspx盒子的测试。我知道如何添加一个正则表达式,但我完全没有尝试为此制作一个正则表达式。如果有人可以帮忙解释一下我会真的很感激

Hello.

Can anyone tell me how to make a regex for this problem. I have an issue with currency where sometimes people by mistake types a '.' instead of a ',' However it should be fine to use the '.' to seperate numbers

1,0
1.0 <- fail
1,92
1.92 <- fail
100,2
100.2 <- fail
100,02
100.02 <-- fail
1.000,00
1.000.00 <- fail
1.000.000,01
1.000.000.02 <- fail

The plan is to use it as a testing on an aspx box. I know how to add a regex but i fail utterly in trying to make a working regex for this. If anyone can help out with an explanation i would truly appreciate it

推荐答案

在.NET中使用build in函数可能就是这样:

Using the build in functions in .NET might be the way to go:
using System;
using System.Globalization;
class Sample
{
public static void Main()
{
CultureInfo ciClone = null;
double value = 0;
try {
ciClone = (CultureInfo)CultureInfo.InvariantCulture.Clone();
ciClone.NumberFormat.NumberDecimalSeparator = ",";
ciClone.NumberFormat.NumberGroupSeparator = ".";
value = Double.Parse("1.000,05", ciClone);
Console.WriteLine("The value is {0}", value);
}

catch (Exception e) {
Console.WriteLine(e);
}
}
}







如果你想使用正则表达式你可以试试这个:

Expresso - 用于构建和测试正则表达式的工具 [ ^ ]



并自行构建一个。




If you want to use regex you could try this:
Expresso - A Tool for Building and Testing Regular Expressions[^]

and build one for yourself.


试试这个:

Try this:
\.\d+


如果它匹配,根据你的定义是错误的。



正如肯尼斯所说:得到一份Expresso!可爱的软件......

If it matches, it's wrong according to your definitions.

As Kenneth said : get a copy of Expresso! Lovely piece of software...


这篇关于你如何制作正则表达式只有在使用逗号时才会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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