十进制值的正则表达式限制 16 位
regex for decimal value restrict 16 digit
我想要一个正则表达式来限制最大输入十进制值。 16位或15位加一个字符(包括小数点)
我在 Regex 下面发现它在 C# 代码中可以正常工作,但是当我在 TextEdit xaml 中使用它作为掩码时。 (DevExpress) 抛出异常 syntax error
:
掩码:
^(?:(?=.{0,16}$)\d*\.\d+|\d{0,16})[kKmMbBtT]?$
文本编辑Xaml:
<dxe:TextEdit HorizontalAlignment="Left" MaskType="RegEx"
Mask="(?:(?=.{0,16}$)[0-9]*([.]?[0-9]+)|[0-9]{0,16})[kKmMbBtT]?"
VerticalAlignment="Top" Width="150"
EditValue="{Binding DecValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="10,33,0,0"/>
我想从中达到的目的:
- 用户可以输入16位十进制值(包括小数点)或
- 用户可以输入15位数字和一个字符(包括小数点)
- 他只能输入一次小数点
- 输入字符串的总长度不得超过 16 个字符。
Extended Regular Expressions provide almost unlimited flexibility to
create input masks. The syntax used by masks in this mode is similar
to the syntax defined by the POSIX ERE specification. Back referencing
is not supported.
因此,您不能使用 (?: <em>subexpression</em>)
或 (?= <em>subexpression</em>)
等。你可以使用一些奇怪的掩码,如下所示:
\d{0,16}|\d{14}\R.\d{1}|\d{13}\R.\d{1,2}|\d{12}\R.\d{1,3}|\d{11}\R.\d{1,4}|\d{10}\R.\d{1,5}|\d{9}\R.\d{1,6}|\d{8}\R.\d{1,7}|\d{7}\R.\d{1,8}|\d{6}\R.\d{1,9}|\d{5}\R.\d{1,10}|\d{4}\R.\d{1,11}|\d{3}\R.\d{1,12}|\d{2}\R.\d{1,13}|\d{1}\R.\d{1,14}|\R.\d{1,15}
在你的 XAML 中:
<dxe:TextEdit HorizontalAlignment="Left" MaskType="RegEx"
Mask="\d{0,16}|\d{14}\R.\d{1}|\d{13}\R.\d{1,2}|\d{12}\R.\d{1,3}|\d{11}\R.\d{1,4}|\d{10}\R.\d{1,5}|\d{9}\R.\d{1,6}|\d{8}\R.\d{1,7}|\d{7}\R.\d{1,8}|\d{6}\R.\d{1,9}|\d{5}\R.\d{1,10}|\d{4}\R.\d{1,11}|\d{3}\R.\d{1,12}|\d{2}\R.\d{1,13}|\d{1}\R.\d{1,14}|\R.\d{1,15}"
VerticalAlignment="Top" Width="150"
EditValue="{Binding DecValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="10,33,0,0"/>
我想要一个正则表达式来限制最大输入十进制值。 16位或15位加一个字符(包括小数点)
我在 Regex 下面发现它在 C# 代码中可以正常工作,但是当我在 TextEdit xaml 中使用它作为掩码时。 (DevExpress) 抛出异常 syntax error
:
掩码:
^(?:(?=.{0,16}$)\d*\.\d+|\d{0,16})[kKmMbBtT]?$
文本编辑Xaml:
<dxe:TextEdit HorizontalAlignment="Left" MaskType="RegEx"
Mask="(?:(?=.{0,16}$)[0-9]*([.]?[0-9]+)|[0-9]{0,16})[kKmMbBtT]?"
VerticalAlignment="Top" Width="150"
EditValue="{Binding DecValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="10,33,0,0"/>
我想从中达到的目的:
- 用户可以输入16位十进制值(包括小数点)或
- 用户可以输入15位数字和一个字符(包括小数点)
- 他只能输入一次小数点
- 输入字符串的总长度不得超过 16 个字符。
Extended Regular Expressions provide almost unlimited flexibility to create input masks. The syntax used by masks in this mode is similar to the syntax defined by the POSIX ERE specification. Back referencing is not supported.
因此,您不能使用 (?: <em>subexpression</em>)
或 (?= <em>subexpression</em>)
等。你可以使用一些奇怪的掩码,如下所示:
\d{0,16}|\d{14}\R.\d{1}|\d{13}\R.\d{1,2}|\d{12}\R.\d{1,3}|\d{11}\R.\d{1,4}|\d{10}\R.\d{1,5}|\d{9}\R.\d{1,6}|\d{8}\R.\d{1,7}|\d{7}\R.\d{1,8}|\d{6}\R.\d{1,9}|\d{5}\R.\d{1,10}|\d{4}\R.\d{1,11}|\d{3}\R.\d{1,12}|\d{2}\R.\d{1,13}|\d{1}\R.\d{1,14}|\R.\d{1,15}
在你的 XAML 中:
<dxe:TextEdit HorizontalAlignment="Left" MaskType="RegEx"
Mask="\d{0,16}|\d{14}\R.\d{1}|\d{13}\R.\d{1,2}|\d{12}\R.\d{1,3}|\d{11}\R.\d{1,4}|\d{10}\R.\d{1,5}|\d{9}\R.\d{1,6}|\d{8}\R.\d{1,7}|\d{7}\R.\d{1,8}|\d{6}\R.\d{1,9}|\d{5}\R.\d{1,10}|\d{4}\R.\d{1,11}|\d{3}\R.\d{1,12}|\d{2}\R.\d{1,13}|\d{1}\R.\d{1,14}|\R.\d{1,15}"
VerticalAlignment="Top" Width="150"
EditValue="{Binding DecValue, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Margin="10,33,0,0"/>