Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to regular expression patter for Numbers

i have a form in which there are two textbox textbox1 , textbox2
textbox1 should accept value between 0 to 12 only and textbox2 should accept value between 30 to 300 only

i have this pattern tried not working pls help by providing the patterns for this two textbox 1 and textbox2

my pattern syntax

([0-9][2]|{1,12})$


What I have tried:

([0-9][2]|{1,12})$

i have tried this expression not working pls help out
providing pattern
Posted
Updated 31-Aug-18 4:02am
v2
Comments
Richard Deeming 31-Aug-18 10:58am    
You've tagged this question as both "C#" and "Java". Those are two completely separate languages. Which one are you using?

Try this:
^[0-9]$|^1[0-2]$

for the first one.
You should be able to figure out the other one.
The 30 Minute Regex Tutorial[^]
 
Share this answer
 
Comments
Member 12663465 31-Aug-18 7:34am    
Thank u sir
sir can u pls help out with second
pattern accept only within range 30 to 300
Peter Leow 31-Aug-18 8:10am    
^[3-9][0-9]$|^[1-2][0-9][0-9]$|^300$
You have to parse the inputs from left to right for all possible numbers of digits.

For 0 to 12 this would be 0 to 9 and 10 to 12. So a possible regex might be
([0-9])|(1[0-2])

For 30 to 300 with 30 to 99, 100 to 299, and 300:
([3-9][0-9])|([1-2][0-9][0-9])|(300)

However, when using this in a validation function it would be much easier and faster to use if conditions after getting and optionally trimming the input (which has to be done for the regex too) and converting to integer.
 
Share this answer
 
Comments
Member 12663465 31-Aug-18 7:32am    
this one is not working properly
([3-9][0-9])|([1-2][0-9][0-9])|(300)
help out
Jochen Arndt 31-Aug-18 7:51am    
I have omitted the begin and end markers. I just provided the plain matching parts because I have no information which regex engine and method is used. That depends also on the used programming language which might provide functions that already check the whole string.

Try this:
^(([3-9][0-9])|([1-2][0-9][0-9])|(300))$
Quote:
([0-9][2]|{1,12})$
i have tried this expression not working

The first problem here is that {1,12} in your RegEx means nothing, it is not legal in this context.
Second problem, your RegExs is missing ^ at beginning, which mean that any thing that ends with what you want will match.
ex: Anything10 will match.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
 
Share this answer
 
Rather than use a regular expression, have you considered calling setFilters() on the EditText object? Something like:
EditText et = (EditText) findViewById(R.id.edit1);
et.setFilters(new InputFilter[]{ new InputFilterRange("0", "12")});
...
et.setFilters(new InputFilter[]{ new InputFilterRange("30", "300")});
Of course, you'll need to create a InputFilterRange class by extending InputFilter.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900