RegEx with < or >
-
Hello, I am trying to setup a RegEx expresion to check a text box for numeric values but also have the possibility to have a < or > symbol. For example the input could be 25.1 or > 20 or < 50, etc. How can I allow for the < or > symbol? Thanks
There is a Regular Expressions forum. How about something as simple as:
^(\s|\d|\.|<|>)*$
(not tested). -
Hello, I am trying to setup a RegEx expresion to check a text box for numeric values but also have the possibility to have a < or > symbol. For example the input could be 25.1 or > 20 or < 50, etc. How can I allow for the < or > symbol? Thanks
If you don't want to allow any spaces, try
^[<>]?\d+\.?\d*$
That scans as^ beginning of string
[<>]? < or >, zero or one times
\d+ one or more digits
\.? zero or one decimal points
\d* zero or more digits
$ end of stringRan a few cases past Expresso and they look OK. If you want to allow scientific notation, etc, grab the examples from Expresso (see our Free Tools forum for a link) and splice in the
[<>]?
bit. This one won't allow.3
or>.6
- it requires a digit before a decimal point, but a few minutes with Expresso will get you anywhere. Cheers, Peter ps As the previous answer indicated, the Regular Expressions forum would be more appropriate next time.Software rusts. Simon Stephenson, ca 1994.
-
If you don't want to allow any spaces, try
^[<>]?\d+\.?\d*$
That scans as^ beginning of string
[<>]? < or >, zero or one times
\d+ one or more digits
\.? zero or one decimal points
\d* zero or more digits
$ end of stringRan a few cases past Expresso and they look OK. If you want to allow scientific notation, etc, grab the examples from Expresso (see our Free Tools forum for a link) and splice in the
[<>]?
bit. This one won't allow.3
or>.6
- it requires a digit before a decimal point, but a few minutes with Expresso will get you anywhere. Cheers, Peter ps As the previous answer indicated, the Regular Expressions forum would be more appropriate next time.Software rusts. Simon Stephenson, ca 1994.
:thumbsup:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
If you don't want to allow any spaces, try
^[<>]?\d+\.?\d*$
That scans as^ beginning of string
[<>]? < or >, zero or one times
\d+ one or more digits
\.? zero or one decimal points
\d* zero or more digits
$ end of stringRan a few cases past Expresso and they look OK. If you want to allow scientific notation, etc, grab the examples from Expresso (see our Free Tools forum for a link) and splice in the
[<>]?
bit. This one won't allow.3
or>.6
- it requires a digit before a decimal point, but a few minutes with Expresso will get you anywhere. Cheers, Peter ps As the previous answer indicated, the Regular Expressions forum would be more appropriate next time.Software rusts. Simon Stephenson, ca 1994.
Thanks everyone for the suggestions. They have helped get started but the criteria has changed. I now need to support the following conditions: 1. a value of N/A only in the field => N/A 2. a numeric value with decimal points => 24.231 3. a numeric value that can begin with < character => < 12.1, or < .5 4. a numeric value that can begin with > character => > 27.2, or > .5
-
Thanks everyone for the suggestions. They have helped get started but the criteria has changed. I now need to support the following conditions: 1. a value of N/A only in the field => N/A 2. a numeric value with decimal points => 24.231 3. a numeric value that can begin with < character => < 12.1, or < .5 4. a numeric value that can begin with > character => > 27.2, or > .5
Before we go any further, let's settle on the spec. Is
N/A
required to be uppercase? Are any or all ofn/a N/a n/A
acceptable? From your example,.12
is OK. What about13.
? Is an integer value like123
(no decimal point) allowed? Are there limits on the number of digits (total/before/after the decimal point)? Your answers to these questions do not affect the solvability of the problem (the existence of a suitable regex), but they do affect the solution (the value of the regex). Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
-
Before we go any further, let's settle on the spec. Is
N/A
required to be uppercase? Are any or all ofn/a N/a n/A
acceptable? From your example,.12
is OK. What about13.
? Is an integer value like123
(no decimal point) allowed? Are there limits on the number of digits (total/before/after the decimal point)? Your answers to these questions do not affect the solvability of the problem (the existence of a suitable regex), but they do affect the solution (the value of the regex). Cheers, PeterSoftware rusts. Simon Stephenson, ca 1994.
Basically, I have a field that either can take an integer value up to 3 digits or a decimal value with up to 2 decimal places. I change the mask in code before the screen is displayed. Below is an example of the existing masks. TextBox1.Mask ='\d?\d?\d?'; OR TextBox1.Mask = '\d+.\d?\d?'; Now, I need to have the integer mask have the ability to accept a <, > or N/A as well as to have the decimal mask do the same. I need it in two masks because I dont want certain fields to be able to enter decimal values and other fields to have the ability. I also can set the textbox to foce upper case for (N/A) and set a max length if needed.
-
Basically, I have a field that either can take an integer value up to 3 digits or a decimal value with up to 2 decimal places. I change the mask in code before the screen is displayed. Below is an example of the existing masks. TextBox1.Mask ='\d?\d?\d?'; OR TextBox1.Mask = '\d+.\d?\d?'; Now, I need to have the integer mask have the ability to accept a <, > or N/A as well as to have the decimal mask do the same. I need it in two masks because I dont want certain fields to be able to enter decimal values and other fields to have the ability. I also can set the textbox to foce upper case for (N/A) and set a max length if needed.
OK. Here are a couple: *note* these are regexes, which may or may not be suitable for use directly in a maskedit control mask field. integer, 1 - 3 digits with N/A < >
^(N/A)|([<>]?\d{1,3})$
float, 0 - 2 decimal places, with the same extras^(N/A)|([<>]?\d+\.\d{0,2})$
This one requires at least one digit before the decimal point. If you want to allow.12
change the+
in the middle to*
. Following the "teach a man to fish" philosophy, I strongly recommend you get yourself a copy of the free tool Expresso[^] (I have no connection to Ultrapico other than as a very satisfied user.) Cheers, Peter [edit] added *note* [/edit]Software rusts. Simon Stephenson, ca 1994.
modified on Wednesday, August 3, 2011 9:17 PM
-
OK. Here are a couple: *note* these are regexes, which may or may not be suitable for use directly in a maskedit control mask field. integer, 1 - 3 digits with N/A < >
^(N/A)|([<>]?\d{1,3})$
float, 0 - 2 decimal places, with the same extras^(N/A)|([<>]?\d+\.\d{0,2})$
This one requires at least one digit before the decimal point. If you want to allow.12
change the+
in the middle to*
. Following the "teach a man to fish" philosophy, I strongly recommend you get yourself a copy of the free tool Expresso[^] (I have no connection to Ultrapico other than as a very satisfied user.) Cheers, Peter [edit] added *note* [/edit]Software rusts. Simon Stephenson, ca 1994.
modified on Wednesday, August 3, 2011 9:17 PM