简体   繁体   中英

regex for decimal value restrict 16 digit

I want a regex expression to restrict input decimal value at max. 16 digits or 15 digits and one character (including decimal point)

I found below Regex it is working find in C# code but when i am using it in TextEdit xaml as mask. (DevExpress) throwing exception syntax error :

例外

Mask:

^(?:(?=.{0,16}$)\d*\.\d+|\d{0,16})[kKmMbBtT]?$

TextEdit 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"/>

Purpose I want to achieve from it:

  • User can enter at 16 digits decimal value (including decimal point) or
  • user can enter 15 digit and one character (including decimal point)
  • He can enter only decimal point one time
  • Total length of input string must not more than 16 characters.

According to documentation :

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.

So, you cannot use grouping constructs such (?: subexpression ) or (?= subexpression ) etc. You can use some weird mask like this:

\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}

And in your 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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM