Regular Expression - regex

I'm trying to get the regular expression to work (using jQuery) for a specific pattern I need.
I need following pattern:
First two character
s of the string need to be numbers (0-9) but maximum number is 53. for numbers below 10 a leading 0 is required
Character on position 3 needs to be a .
the next 4 characters need to be a number between 0-9, minimum number should be 2010, maximum 2050
so, Strings like 01.2020, 21.2020, or 45.2020 have to match but 54.2020 or 04.2051 must not.
I tried to write the regex without the min and max requirement first and I'm testing the string using regex101.com but I'm unable to get it to work.
acording to the definition /^[0-9]{2}\.\d[0-9]{4}$/ should allow me to insert the strings in the format NN.NNNN.
thankful for any input.

2 numbers from 00 to 53 can be matched using this : (?:[0-4][0-9]|5[0-3]) (00 -> 49 or 50 -> 53)
Character on position 3 needs to be a . : you've already got the \.
a number between 2010 and 2050 -> 20(?:[1-4][0-9]|50) (20 followed by either 10 -> 49 or 50)
This gives :
(?:[0-4][0-9]|5[0-3])\.20(?:[1-4][0-9]|50)

Related

Regex needed to match a proper currency format of whole numbers, and set maximum dollar value

I'm trying to create a regex that enforces:
whole numbers only, no decimals/fractions
thousands separated by commas
sets a maximum value allowed. Acceptable range of 1-25,000,000,000 (25 billion)
I created the following regex that already accomplishes the first 2 requirements, only allowing acceptable values like:
1
1,000
25,000
250,000,000 etc.
but it's the 3rd requirement of setting a maximum value of 25 billion that I'm struggling with.
Does anyone know a way to enhance this current pattern to only allow values between the range of 1 - 25,000,000,000 ?
^[1-9]\d?\d?$|^(?!0,)(?!0\d,)(?!0\d\d,)(\d\d?\d?,)+\d{3}$
I did a lot of searching, and I found a regex that could impose a maximum value, but I can't quite figure out how to modify it to what I need to meet all 3 requirements. This is the one I found:
^((25000000000)|(2[0-4][0-9]{9})|(1[0-9]{10})|([1-9][0-9]{9})|([1-9][0-9]{8})|([1-9][0-9]{7})|([1-9][0-9]{6})|([1-9][0-9]{5})|([1-9][0-9]{4})|([1-9][0-9]{3})|([1-9][0-9]{2})|([1-9][0-9]{1})|([1-9]))$
I think this should do the trick:
^([1-9]\d{0,2}(,\d{3}){0,2})$|^(([1-9]|1\d|2[1-4])(,\d{3}){3})$|^25(,000){3}$
This regex consist of 3 main blocks or conditions:
[1-9]\d{0,2}(,\d{3}){0,2}: Any 1-9 followed by up to 2 digits, followed by up to 2 optional blocks of 3 digits preceded with a comma (supports up to 999,999,999).
([1-9]|1\d|2[1-4])(,\d{3}){3}: Three possible billion values: 1-9, or a 1 followed by any digit (to support 10-19), or a 2 followed by a 1-4 digit (to support 20-24). Then followed by 3 blocks of comma and 3 digits (supports up to 24,999,999,999).
25(,000){3}: Finally, special case, support for 25,000,000,000.
It matches:
1
12
123
1,000
25,000
250,000
2,500,000
24,999,999
25,000,000
250,000,000
1,500,000,000
2,500,000,000
15,000,000,000
24,999,999,999
25,000,000,000
And does not match:
0
1234
0,000
0,000,999
0,999,999,999
25,000,000,001
99,999,999,999
250,000,000,000
25,000,000,000,000
99,99,999
9,9,9,9,999
24999999999
25000000000
25000000001
26000000000
35000000000

I need regex to validate a pattern

I need regex to validate a pattern,
pattern can be combination of following list,
yyyy : current year
yy : last 2 digits of current year
MM : current month
dd : current day
HH : current hour
mm : current minute
ss : current second
fff : current milisecond
ff : current 1/100th of second
R : random digits
Rules
Minimum 16 and maximum 20 characters can be allowed.
We should be able to add fix characters only in capitals(e.g. 'yyyyAAddmmss' here AA is fix value)
We should escape a fix character(e.g. \R, then R is not replaced)
Only HMR characters should be allowed to escape like '\H' or '\M' or '\R' as this characters are in the pattern.
Pattern should contains minimum 4 random digits (e.g. yyRMMRddRHHRmmRss)
It should allow digits 0-9
It should not allow special characters.
Example of such valid pattern
yyyyMMddHHmmssff
yyyyMMddHHmmssffR
AByyyyMMddHHmmssRRRR
AByyyy\RMMddHHRRmmRR
\Myy\HMMdd\RRHHmmss99RR
What is ff? Is it current-milliseconds/100 or current-seconds/100?
If It's current-milliseconds/100 use this regex
\d{2}(\d{2})(\w{3})(\d{2})(\d{2})(\d{2})(\d{13})(\d{11}.\d{2})(\d+)
Pattern should contains minimum 4 random digits (e.g. yyRMMRddRHHRmmRss)
You are asking for something that may not yield one unique match.
For example, the pattern 20110221318763521 could be matched in many ways:
20110221318323521
yyRMMRddRHHRmmRss => 20-10-21 18:23:21
yyMMRRddRHHRmmRss => 20-11-21 18:23:21
yyRRMMddRHHRmmRss => 20-02-21 18:23:21
yyRMMRddRHHmmRssR => 20-10-21 18:32:52
...

Regular expression to validate 2 character hex string

I have a source of data that was converted from an oracle database and loaded into a hadoop storage point. One of the columns was a BLOB and therefore had lots of control characters and unreadable/undetectable ascii characters outside of the available codeset. I am using Impala to write regex replace function to parse some of the unicode characters that the regex library cannot understand. I would like to remove the offending 2 character hex codes BEFORE I use the unhex query function so that I can do the rest of the regex parsing with a "clean" string.
Here's the code I've used so far, which doesn't quite work:
'[2-7]{1}([A-Fa-f]|[0-9]{1})'
I've determined that I only need to capture \u0020-\u007f - or represented in the two bit hex - 20-7f
If my string looks like this:
010A000000153020405C00000000143020405CBC000000F53320405C4C010000E12F204058540100002D01
I would like to be able to capture 2 characters at a time (e.g. 01,0A,00) evaluate whether or not that fits the acceptable range of 2 byte hex I mentioned above and return only what is acceptable.
The correct output should be:
30 20 40 5C 30 20 40 5C 33 20 40 5C 4C 2F 20 40 58 and 54
However, my expression finds the first acceptable number in my first range (5) and starts the capture from there which returns the position or indexing wrong for the rest of the string... and this is the return from my expression -
010A0000001**53**0**20****40****5C**000000001**43**0**20****40****5C**BC000000F**53****32**0**40****5C****4C**010000E1**2F****20****40****58****54**010000**2D**01
I just don't know how to evaluate only two characters at a time in a mixed-length string. And, if they don't fit the expression, iterate to the next two characters. But only in two character increments.
My example: https://regex101.com/r/BZL7t0/1
I have added a Positieve Lookbehind to it. Which starts at the beginning of the string and then matches 2 characters at the time. This ensures that the group you're matching always has groups of 2 characters before it.
Positieve Lookbehind:
(?<=^(..)*)
Updated regex:
(?<=^(..)*)([2-7]{1}[A-Fa-f0-9]{1})
Preview:
Regex101

Regular Expression - Two Digit Range (23-79)?

I have been reading the regex questions on this site but my issue seems to be a bit different. I need to match a 2 digit number, such as 23 through 75. I am doing this on an HP-UX Unix system. I found examples of 3 - 44 but or any digit number, nothing that is fixed in length, which is a bit surprising, but perhaps I am not understand the variable length example answer.
Since you're not indicating whether this is in addition to any other characters (or in the middle of a larger string), I've included the logic here to indicate what you would need to match the number portion of a string. This should get you there. We're creating a range for the second numbers we're looking for only allowing those characters. Then we're comparing it to the other ranges as an or:
(2[3456789]|[3456][0-9]|7[012345])
As oded noted you can do this as well since sub ranges are also accepted (depends on the implementation of REGEX in the application you're using):
(2[3-9]|[3-6][0-9]|7[0-5])
Based on the title you would change the last 5 to a 9 to go from 75-79:
(2[3-9]|[3-6][0-9]|7[0-9])
If you are trying to match these numbers specifically as a string (from start to end) then you would use the modifiers ^ and $ to indicate the beginning and end of the string.
There is an excellent technical reference of Regex ranges here:
http://www.regular-expressions.info/numericranges.html
If you're using something like grep and trying to match lines that contain the number with other content then you might do something like this for ranges thru 79:
grep "[^0-9]?(2[3-9]|[3-6][0-9]|7[0-9])[^0-9]?" folder
This tool is exactly what you need: Regex_For_Range
From 29 to 79: \b(2[3-9]|[3-7][0-9])\b
From 29 to 75: \b(29|[3-6][0-9]|7[0-5])\b
And just for fun, from 192 to 1742: \b(19[2-9]|[2-9][0-9]{2}|1[0-6][0-9]{2}|17[0-3][0-9]|174[0-2])\b :)
If I want 2 digit range 0-63
/^[0-9]|[0-5][0-9]|6[0-3]$/
[0-9] will allow single digit from 0 to 9
[0-5][0-9] will allow from 00 to 59
6[0-3] will allow from 60 till 63
This way you can take Regular Expression for any Two Digit Range
You have two classes of numbers you want to match:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 7, followed by any digit
Edit: Well, that's the title's range (23-79). Within your question (23-75), you have three:
the digit 2, followed by one of the digits between 3 and 9
one of the digits between 3 and 6, followed by any digit
the digit 7, followed by one of the digits between 0 and 5
Just to add to this, here is a solution for generating the string from the accepted answer in javascript. You can click "Run Code Snippet" to enter your own bounds and get your own string.
function regexRangeString(lower,upper){
let current=lower;
let nextRange=function(){
let currentString=String(current);
let len=currentString.length;
let string="";
let newUpper;
for(let digit=0;digit<len;digit++){
let index=len-digit-1;
let lower=Number(currentString[index]);
let thisString="";
for(let u=9;u>=lower;u--){
let us=currentString.substring(0,index)+u+currentString.substring(index+1,len);
if(Number(us)<=upper){
if(lower==u){
thisString=lower;
}
else{
thisString=`[${lower}-${u}]`;
}
currentString=currentString.substring(0,index)+u+currentString.substring(index+1,len);
break;
}
}
if(thisString!="[0-9]"){
string=currentString.substring(0,index)+thisString+string;
break;
}
else{
string=thisString+string
}
}
current=Number(currentString)+1;
return string
}
let string=""
while(current<upper){
string+="|"+nextRange(current);
}
string="("+string.slice(1)+")";
return string
}
let lower=prompt("Enter Lower Bound")
let upper=prompt("Enter Upper Bound")
alert(regexRangeString(lower,upper))
For example:
regexRangeString(72,189)
generates the following output string:
(7[2-9]|[8-9][0-9]|1[0-8][0-9])
This should do it:
/^([2][3-9]|[3-6][0-9]|[7][0-5])$/
^ and $ will make it strict that it will match only 2 numbers, so in case that you have i.e 234 it won't work.

regex for phone number validation?

i want to validate my phone number with the regex for following formats.i have googled the things but i could not find the regex for following formats...
079-26408300 / 8200
(079) 26408300
079 264 083 00
9429527462
can anyone please guide me how can i do validate the phone number field for above formats?
I want to validate the phone number for only above formats as right now am using only following regex var phone_pattern = /^[a-z0-9]+$/i;
#Ali Shah Ahmed
var phone_pattern = "(\d{10})|(\d{3}-\d{8}\s/\s\d{4})|((\d{3}\s){3}\d{2})|((\d{3})\s\d{8})";
here is the way am checking if its valid
if (!phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
} else {
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
}
its not working. Am I implementing in wrong way?
lets start with the simplest phone number 9429527462
As this has 10 characters and all are numbers, regex for it could be \d{10}
Now the next phone number 079 264 083 00. Regex for this pattern could be (\d{3}\s){3}\d{2}
First we are expecting a group of 3 digits and a space to repeat thrice (\d{3}\s){3}, this will cover 079 264 083 (space included in it), so left will be the last two characters which are handled using \d{2}
For the phone number (079) 26408300, \(\d{3}\)\s\d{8} regex could be use. The regex first looks for a opening bracket, then three digits inside it, and then the closing bracket. It then looks for a space, and then for 8 digits.
The phone number 079-26408300 / 8200 could be validated using regex \d{3}-\d{8}\s/\s\d{4}. It first looks for 3 digits then a -, then 8 digits followed by a space. Then looks for a / and then a space and then 4 digits.
If you wish to know a single regex for validating all the above patterns, do let me know.
Final combined regex would be:
/(\d{10})|(\d{3}-\d{8}\s\/\s\d{4})|((\d{3}\s){3}\d{2})|(\(\d{3}\)\s\d{8})/
Straightforward solution is simple, use |
String ex = "\\d{3}-\\d{8} / \\d{4}|\\(\\d{3}\\) \\d{8}|...