RegEx匹配點之前的2位數和點之后的一位數(僅0或5)

[英]RegEx to match 2 digits before the dot and one digit after the dot(just 0 or 5)


I need a regular expression that will match one or two digits before the dot and one digit after the dot (0 or 5).

我需要一個正則表達式,它將匹配點之前的一位或兩位數以及點之后的一位數(0或5)。

I tried it out for at least one hour, and I'm getting mad....

我嘗試了至少一個小時,我生氣了......

Possible results should be:

可能的結果應該是:

5,0
5,5
30,0
30,5 etc.

If just one digit is insert, it want a result as follows :

如果只插入一個數字,則需要如下結果:

5 --> 5,0

5 - > 5,0

Is there someone who can help me? Thanks a lot!!

有人可以幫助我嗎?非常感謝!!

3 个解决方案

#1


2  

You just want to check for one or two digits, followed by the dot literal, and either '0' or '5'.

您只想檢查一個或兩個數字,然后是點文字,以及“0”或“5”。

^\d{1,2}\.[05]$

That doesn't handle the single digit one, though. There's not an easy way to just match a single digit in the same regex you're matching one or two, so you could use a second regex:

但是,這並不能處理單個數字。在匹配一個或兩個匹配的同一個正則表達式中匹配單個數字並不是一種簡單的方法,因此您可以使用第二個正則表達式:

^\d$

Then convert that to a double/float if you get any matches.

如果你得到任何匹配,然后將其轉換為double / float。

#2


1  

Try the following regex...

試試以下正則表達式......

(?:\d{1,2}(?=.)|(?<=.)[50])

(?:??\ d {1,2}(=)|(<=)[50])

#3


0  

string regex=@"(?<b>\d{1,2})(?<a>[.]0|[.]5)?";
Match m=Regex.Match(input,regex);
string result=m.Groups["b"].Value+","+m.Groups["a"].Value==""?0:m.Groups["a"].Value;

Above code would give these results for input

上面的代碼會給出這些輸入結果

550.57 => 50,5    
644 => 44,0

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/10/09/7256b72edc920effec555cf4800e5122.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com