English 中文(简体)
对有限性别的登记
原标题:RegEx for limited decimal
  • 时间:2011-10-26 14:58:24
  •  标签:
  • c#
  • regex

New to regex syntax here. 努力写一个登记册,以提供一些意见验证。

What I need is a regex to match a whole number, or a decimal with exactly one digit past the decimal.

Good Match
1
12
100
1.1
100.1
1.0

No Match
1.22
1.
0
012

这里是我来的,但我没有做什么工作:

Regex.IsMatch(paidHours, "\d+[.]?[0-9]?")
最佳回答

www.un.org/Depts/DGACM/index_spanish.htm Edited response after OP question edit.

Regex.IsMatch(paidHours, @"^[1-9][0-9]*(.[0-9])?$");

解释:

^      : Start of the String
[1-9]  : A single number between 1 and 9
[0-9]* : Zero or more number(s) between 0 and 9 
         ([0-9]? would match zero or one number and the String "100" would not match the regex)
(      : Start of a group
.     : A point
[0-9]  : A single number between 0 and 9
)?     : End of the group. The group must be repeated zero or one time
$      : End of the String

请注意,d is not exactlyequi to [0-9]:dcompes any rel=“nofollow”>unicode 数/a>。 例如,这一特性<代码>。 如果您使用<代码>d,那么如果您使用<代码>[0-9],则将获得相应数据。

问题回答

You can try with:

Regex.IsMatch(paidHours, "^\d+(\.\d)?$")
Regex.IsMatch(paidHours, @"^d+(.d)?$")

具体列明行乞/行乞:

@"^d+[.]?[0-9]?$"

如果你没有具体指明你希望打字端标有<代码>签字,你就会赢得笔记。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签