Search and validate a year (four continuous digits) in text - regex

How do I validate that a string has only 4 contiguous digits or no digits?
I know that /\d{4}/ will validate 4 contiguous digits, but if my string is dh25ah1233dadh3, it must be invalid.
Valid strings
dkskdsokd
adad dadad
addad1257adada
1587dadad
sasasas7854
Invalid strings
dh25ah1233dadh3
fsdfdfd1982fdf2
1some1422dd

If I understand the question, it'd be something like:
/^(?:\D*\d{4}\D*)*$/
That matches any number of the sub-pattern "some non-digits, then 4 digits, then some more non-digits".
If you want only one group of 4 digits, it'd be
/^\D*(?:\d{4})?\D*$/
edit — if you want the first one, and you don't want groups of 8 (or 12 or 16 ...) digits to be accepted, you'd do something like this:
/^(?:\D*\d{4}\D+)*$/
edit let's try this again:
/^(?:\D*\d{4}\D+)*(?:\D*\d{4})?$/
That allows the string to end with a 4-digit group.

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

Regx for string 2 digits and 10 alphabets irrespective of their position in string

Regular expression in java for a string which can contain 2 digits & 10 alphabets irrespective of their position in String
Examples of string are:
1abcdefghij2
12abcdefghij
abcdefghij12
abcdefg1hij2
ab12cdefghij
Is it possible?
I think the regex you are looking for is like this.
Regex: ^(?=\D*\d\D*\d\D*$)[a-zA-Z0-9]{12}$
Explanation:
(?=\D*\d\D*\d\D*$) checks for presence of 2 digits.
[a-zA-Z0-9]{12} makes sure that the total length is 12.
Since presence of 2 digits is already checked obviously there will be 10 alphabets.
Regex101 Demo
Edit #1: Edited regex on Sebastian Proske's advice from
^(?=.*\d.*\d)[a-z0-9]{12}$ to ^(?=\D*\d\D*\d\D*$)[a-zA-Z0-9]{12}$
Yes, it's possible.
[12a-j]+ for strings not limited by length and [12a-j]{12} for string exactly 12 characters long.
You can test it here.

Regex to match digits with decimals that have spaces as well as no spaces

I am looking for a regex string to match a set of numbers:
9.50 (numbers without spaces, that have 2 to 4 decimal points)
1 9 . 5 0 (numbers with spaces that have 2 to 4 decimals points)
10 (numbers without spaces and without decimal points)
So far I have come up regex string [0-9\s\.]+, but this not doing what I want. Any cleaner solutions out there?
Many Thanks
Try this:
[\d\s]+(?:\.(?:\s*\d){2,4})?
This makes the decimal point and the digits/spaces after it optional. If there are digits after, it checks that there are 2-4 of them with {2,4}
DEMO
If this should only match the whole string, you can anchor it.
^[\d\s]+(?:\.(?:\s*\d){2,4})?\s*$
The problem with your regex is that it will match 127.0.0.1 as well, which is an IP4 address, not a number.
The following regex should do the trick:
[0-9]+[0-9\s]*(\.(\s*[0-9]){2,4})?
Assumption I've made: You need to place at least one digit (before the comma).
regex101 demo.
(\d+[\d\s]*\.((\s*\d){2,4})?|\d+)
I was still getting "trailing spaces" selected with the third example of 10
This eliminated them.
wouldn't this work as well - '[^. 0-9]' ?
my full postgresql query looks like this:
split_part(regexp_replace(columnyoudoregexon , '[^. 0-9]', '', 'g'), ' ', 1)
and its doing the following:
values in the column get everything except numbers, spaces and point(for decimal) replaced with empty string.
split this new char string with split_part() and call which element in the resulting list you want.
was stuck on this for a while. i hope it helps.

Regex for UserName

can you please help me with creating regex having below rules.
Starting and Ending of string do not have any special characters
Allowed special characters are #, - and _ .
immediate 2 special characters are not allowed in string (ie Test..ds, Test_#ds)
String can have maximum 4 special characters
String can have maximum 4 numbers (0-9)
string minimum length is 8 and maximum 50
I tried the regex below, but I don't know how to limit it to four digits.
^[a-zA-Z0-9]((?!(\.|))|\.(?!(_|\.))|[a-zA-Z0-9]){6,18}[a-zA-Z0-9]$
Examples:
Valid String:
User.Name_77
01User_Name_77
UserNameTest
U_ser#Na_m_e
Invalid String
User_Name012345
User__Name
User.#Name
#UserName77
UserName77#
U_ser##Na_me
U_ser#-Na_me
You have a nice spec; you can almost directly transcribe it into positive and negative look aheads (updated based on comment):
^
(?!.*[-#_.]{2}) # no two special in a row
(?!(?:.*[-#_.]){5}) # less than 5 specials
(?!(?:.*\d){5}) # less than 5 digits
(?!^[^a-zA-Z0-9]) # no special at start
(?=.*[a-zA-Z0-9]$) # no specail at end
([-#_.a-zA-Z0-9]{8,50}) #8 to 50 of that char set
$
Demo
Try this:
/^(?!(([A-Za-z0-9]+[\#\.\-\_]){5,}|[A-Za-z0-9]*[\#\.\-\_]{5,}|.{51,}$|.{0,7}$|(.*\d){5,}|.+[\#\.\-\_]{2,}))\b[A-Za-z0-9#._-]*\b$/g
https://regex101.com/r/jX3jS4/7

Word between 9-10 characters, of which 0-2 are numbers

http://regexr.com?32uvo
What I currently have:
\b(?=[A-Z\d]{10})(?:[A-Z]*\d){0,2}[A-Z]*\b
This would only match a string with a length of 10. I would like to change it to between 9 and 10 characters, where 2 can be numbers. Why doesn't this work?
\b(?=[A-Z\d]{9,10})(?:[A-Z]*\d){0,2}[A-Z]*\b
AFAIK, {9,10} should be the length interval.
You were close
\b(?=[A-Z\d]{9,10}\b)(?:[A-Z]*\d){0,2}[A-Z]*\b
--
|->you missed this
try it here
So this regex would match a word that contains 9 to 10 characters[upper case and digits] that contain 1 to 2 digits
if you want to match the whole string you better use ^(start of the string) and $(end of the string)