Regular Exp to validate for Zero's - regex

I have to validate for zero values in my text field, the length of text field is 4 chrs.
If the user enters 0 or 00 or 000 or 0000. this should fail.
presently i have written exp to validate values below 7500 which will also accept 0000.
please help me to tweek this to fail for 0 or 00 or 000 or 0000 values.
/^0*([0-9]{1,3}|[1-6][0-9]{3}|7[0-4][0-9]{2}|7500)$/
Thanks Punith

/^(?:[1-9]\d{0,2}|[1-6]\d{3}|7[0-4]\d{2}|7500)$/
Explanation:
^ # Start of string
(?: # Either match...
[1-9]\d{0,2} # 1-999
| # or
[1-6]\d{3} # 1000-6999
| # or
7[0-4]\d{2} # 7000-7499
| # or
7500 # 7500
) # End of alternation
$ # End of string
If you want to allow leading zeroes, then you can add 0* right after the ^. But then the length restriction to four digits is lost, of course.

Related

hours:minutes:seconds Timestamp regex

I'm trying to validate a user inputed video timestamp in the format hours:minutes:seconds using a regex. So I'm assuming the hours component can be arbitrarily long, so the final format is basically hhhh:mm:ss where there can be any number of h. This is what I have so far
(([0-9]+:)?([0-5][0-9]:))?([0-5][0-9])
where
(
([0-9]+:)? # hh: optionally with an arbitrary number of h
([0-5][0-9]:) # mm: , with mm from 00 to 59
)? # hh:mm optionally
([0-5][0-9]) # ss , wtih ss from 00 to 59
which I believe is almost there, but doesn't handle cases like 1:31 or just 1. So to account for this if I add the first digit inside the mm and ss blocks as optional,
(([0-9]+:)?([0-5]?[0-9]:))?([0-5]?[0-9])
firstly the last seconds block starts matching values like 111. Also values like 1:1:12 are matched , which I don't want (should be 1:01:12). So how can I modify this so that m:ss and s are valid whereas h:m:ss,m:s and sss are not?
I am new to regular expressions, so apologies in advance if I'm doing something stupid. Any help is appreciated. Thanks.
You can match either 1 or more digits followed by an optional :mm:ss part, or match mm:ss.
To also match 6:12 and not 1:1:12 make only the first digit optional in the second part of the pattern.
^(?:\d+(?::[0-5][0-9]:[0-5][0-9])?|[0-5]?[0-9]:[0-5][0-9])$
^ Start of string
(?: Non capture group
\d+ Match 1+ digits
(?::[0-5][0-9]:[0-5][0-9])? Match an optional :mm:ss part, both in range 00 - 59
| Or
[0-5]?[0-9]:[0-5][0-9] Match m:ss or mm:ss both in range 00-59 where the first m is optional
) Close non capture group
$ End of string
Regex demo
Doesn't adding the positional anchors(^ and $)solve your problem?
^(([0-9]+:)?([0-5][0-9]:))?([0-5][0-9])$
Check here: https://regex101.com/r/fRZf2R/1

Regex to Extract Last Part of URL that Contains User ID Strings

I'm having a hard time figuring this one out and could use some help.
I'm using Google Analytics filters to reduce the number of unique pages being reported in our app by stripping out ID strings from the URLs that are coming in.
What I need is a regex that will look for URLs that have these IDs in the URL. Here's what sets them apart from the rest of the URL:
ID strings are always the last part of the URL
ID strings always contain both letters and numbers
ID strings are always either 16- or 32-characters in length
ID strings can show up twice in a URL
ID strings can end with either a "/" or without
Here are some example URLs that show how they appear in our reporting:
/app/6be031b9672be9b5/
/app/admin/client/settings/6be031b9672be9b5
/app/subscribers/ea33fb38c9efc4dc0367819f23434f99/
/app/subscribers/customfieldsettings/0359c487066727ae/
/app/reports/6fa92d36be0e6c16/dc5aa096fba9cbb97eea1dae616d4b3c/
The second part of my question is that this regex should also group everything before these ID strings into a capturing group so that I can call that group later on in the filter, effectively stripping out these ID strings to look like the following:
/app/6be031b9672be9b5/ --> /app/
/app/subscribers/ea33fb38c9efc4dc0367819f23434f99/ --> /app/subscribers/
etc.
I've tried a couple different approaches but none seem to work perfectly, so I could really use the help, thank you!
Here's a solution:
^(.*?)(?:\/[a-zA-Z0-9]{16}|\/[a-zA-Z0-9]{32}){0,2}\/?$
Demo
This will remove the last part or 2 parts of URLs which are 16 or 32 characters long and contain only letters and digits.
You can make sure these parts contain both letters and numbers like this, if the tool supports lookaheads:
^(.*?)(?:\/(?=.{0,15}?\d)(?=.{0,15}?[a-zA-Z])[a-zA-Z0-9]{16}|\/(?=.{0,31}?\d)(?=.{0,31}?[a-zA-Z])[a-zA-Z0-9]{32}){0,2}\/?$
Demo
This adds assertions to the pattern.
Breakdown:
^(.*?) # Start of URL
(?:
\/ # a slash
(?=.{0,15}?\d) # check there's a digit at most 16 chars ahead
(?=.{0,15}?[a-zA-Z]) # check there's a letter at most 16 chars ahead
[a-zA-Z0-9]{16} # check the next 16 chars are digits or letters
| # .. or:
\/ # a slash
(?=.{0,31}?\d) # check there's a digit at most 32 chars ahead
(?=.{0,31}?[a-zA-Z]) # check there's a letter at most 32 chars ahead
[a-zA-Z0-9]{32} # check the next 32 chars are digits or letters
){0,2} # .. at most 2 times
\/?$ # optional slash at end
This will do it:
([a-z0-9]+)(?:\/?$)
Demo
Explanation:
([a-z0-9]+) matches and captures the alphanumeric part
(?:\/?$) looks for (but doesn't match or capture) the optional final / and then the end of the string ($)
modified - totally missed that can be 1 or 2 id's at the end thing.
Oh well, revised fwiw.
# (?i)^(.*?)/((?:(?=[^/]{0,31}[a-f])(?=[^/]{0,31}[0-9])(?:[a-f0-9]{16}|[a-f0-9]{32})(?:(?:/[a-z])?/?$|/)){1,2})$
(?i) # Case insensitive modifier
^ # BOS, begin the ride ..
( .*? ) # (1), Kreep up on the first ID
/ # Trim this / junk
( # (2 start), 1-2 ID's separated by a /
(?:
(?= [^/]{0,31} [a-f] ) # Use largest range (32), Must be a lettr AND number
(?= [^/]{0,31} [0-9] )
(?: # One of 16 or 32 length
[a-f0-9]{16}
| [a-f0-9]{32}
)
(?:
(?: / [a-z] )? # optional / letter
/? $ # /? EOS for end of 1 or 2
| # or,
/ # / between 2 only
)
){1,2}
) # (2 end)
$ # EOS, rides over !!
Sample output:
** Grp 0 - ( pos 195 , len 63 )
/app/reports/6fa92d36be0e6c16/dc5aa096fba9cbb97eea1dae616d4b3c/
** Grp 1 - ( pos 195 , len 12 )
/app/reports
** Grp 2 - ( pos 208 , len 50 )
6fa92d36be0e6c16/dc5aa096fba9cbb97eea1dae616d4b3c/

Regex to validate phone number pattern

How can I construct regx to validate phone number? That is:
First digit must be 04 or 050 , length range between 8-13
First digit cannot be 43 or 44 , first digit must be 4 or 9 and length should be 8 digits
I have tried this pattern:
^[04,050]\\d{8,13}
Can any body help me?
Let's break it down (hoping I understand correctly):
^ # Start of string
(?: # Match one of the following:
04\d{6,11} # Either an 8-13 digit number starting with 04
| # or
050\d{5,10} # an 8-13 digit number starting with 050
| # or
4[0-25-9]\d{6} # an 8 digit number starting with 4 but not 43 or 44
| # or
9\d{7} # an 8 digit number starting with 9
) # End of alternation
$ # End of string

Regular Expression for a string of latitude longitude pairs

I am receiving an input like this in my program
lat1,long1;lat2,long2;lat3,long3
latitude longitude pairs separated by columns.
No I want to validate this input so that i may not receive wrong data.
I have created a regular expression:
((^(\-?\d+(\.\d+)?),(\-?\d+(\.\d+)?));?)
problem is it only validates a single pair not a string of pairs separated by ; as i wish.
If you could help me come up with an expression to validate my data I would be grateful.
Your expression check only one pair at the start of the string folowed by one column.
You can try this :
^\-?\d+(\.\d+)?,\-?\d+(\.\d+)?(;\-?\d+(\.\d+)?,\-?\d+(\.\d+)?)*$
Thats your regex (without some brackets inutiles) folowed by 0 or more times a column and a pair.
A test with your 4 examples.
How about something like this?
(-?[\d\.]+,-?[\d\.]+)+;?+
I tried it on Rubular.com (permalink), with the test string as following:
-10,10;10.5,-10.337
Result?
Match 1
1. -10,10
Match 2
1. 10.5,-10.337
I would use the following pattern, which can also accept values like .2 along with 0.2.
^-?(\d+(\.\d+)?|\.\d+),-?(\d+(\.\d+)?|\.\d+)(;-?(\d+(\.\d+)?|\.\d+),-?(\d+(\.\d+)?|\.\d+)){2}$
Here's the breakdown.
^ # beginning of string
-? # optional minus sign
( # either
\d+(\.\d+)? # an integer with an optional decimal part
| # or
\.\d+ # a decimal number
)
, # a comma
-? # optional minus sign
( # either
\d+(\.\d+)? # an integer and an optional decimal part
| # or
\.\d+ # a decimal number
)
(
; # a semicolon
-? # optional minus sign
( # either
\d+(\.\d+)? # an integer and an optional decimal part
| # or
\.\d+ # a decimal number
)
, # a comma
-? # optional minus sign
( # either
\d+(\.\d+)? # an integer and an optional decimal part
| # or
\.\d+ # a decimal number
)
){2} # with 2 repetitions (of the pattern)
$ # end of string

limit expression length

I am using the following in a script of mine to verify minutes entered... it allows for numbers and a comma for thousands in the correct format only... however, I would like to add a length restriction as well... I can't seem to do it or I'm just putting itin the wrong spot... here is the code as is with no limit :
(!preg_match("#^(\d{1,3}(\,\d{3})*|(\d+))$#",$values['minutes']))
I would like to make this at least one with a max of five... the entry is for minutes online per day... well there are only 1440 minutes in a day... if you entered 1,440 which is valid currently that is 5 characters and I want to limit the expression to that...
Anyone?
Two suggestions:
preg_match("#^(?:\d{1,3}|1,?\d{3})$#"
Explanation:
^ # Start of string
(?: # Either match...
\d{1,3} # a three-digit number
| # or
1 # a four digit number that starts with a 1
,? # and may have a thousands separator
\d{3} # (and three more digits)
)
$ # End of string
The problem is of course that this also allows 1,999, so you'd still need an extra sanity check. This probably is the better solution.
You can also do the range limitation in the regex itself, but that's cumbersome:
preg_match("#^(?:1,?440|1,?4[0-3]\d|1,?[0-3]\d{2}|[1-9]\d{1,2}|\d)$#"
Explanation:
^ # Start of string
(?: # Either match...
1,?440 # 1440
| # or
1,?4[0-3]\d # 1400-1439
| # or
1,?[0-3]\d{2} # 1000-1399
| # or
[1-9]\d{1,2} # 10-999
| # or
\d # 0-9
)
$ # End of string
You're probably better off just testing the string's length or even the integer value. But just to show that it's possible:
preg_match("#^(\d,\d{3}|\d{1,4})$#")
Yes, it's very simple, since a four-digit number can only take one of the forms
one digit, comma, three digits
four digits