Regex for digits and dob [closed] - regex

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am learning regex and don't know much about it. Can someone please tell me what is
the proper regex for:
Exactly 4 digits (1234)
Two digits followed by a slash, then two digits followed by other slash and 4 more digits for date of birth (01/01/1983)

Exactly 4 digits:
\d{4}
Two digits followed by a slash two digits followed by 4 digits for date of birth:
\d{2}/\d{2}/\d{4}
Depending on your use case you may want to add beginning and end of string anchors (^ and $ respectively). For example the regex ^\d{4}$ would match the string 1234, but would not match any part of the string foo 1234. If you wanted to make the digits from foo 1234 you could just use the regex \d{4}.

It depends on the language, but here's the relevant code for javascript:
1) Exactly four digits, with nothing else:
^\d{4}$
2) Two digits followed by a slash two digits followed by 4 digits
^\d{2}/\d{2}/\d{4}$
^ = string must start with this
$ = string must end with this
{n} = where n is the number of times this should be repeated
\d = special escape character for digits
check thisout for more info

Related

How do I get my regular expression to work with 3 numbers or more when two numbers are the same [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The below regular expression works with 012, 201, 102, etc. I am trying to change the regular expression so that it matches 002, 200, 020O from a 4 digit number. I tried varies methods, but the regular expression is matching other patterns. Can someone give me some direction on how to resolve this issue. Thank you.
Working:
RegEx012 = re.compile(r'\b(?=[1-9]*0)(?=[02-9]*1)(?=[013-9]*2)\d+\b')
Not Working:
RegEx002 = re.compile(r'\b(?=[1-9]*0)(?=[1-9]*0)(?=[013-9]*2)\d+\b')
Results:
0250(good)
0260(good)
2052(bad)
2062(bad)
If you want to match a string with 2 times a zero and at least 3 digits, you could use a positive lookahead:
\b(?=[1-9]*0[1-9]*0[1-9]*\b)\d{3,}\b
Explanation
\b Word boundary
(?= Positive lookahead, assert what is on the right contains
[1-9]*0[1-9]*0[1-9]*\b Match 2 times a zero between optional digits 1-9
) Close lookahead
\d{3,} Match 3 or more digits
\b Word boundary
Regex demo
Or the other way around, assert 3 digits and match 2 times a zero between optional digits 1-9
\b(?=\d{3})[1-9]*0[1-9]*0[1-9]*\b
To match when the third character is a 3 (Or use a character class [03] to match either a 0 or 3)
\b(?=[1-9]*0[1-9]*0[1-9]*\b)\d{2}3\d*\b
Regex demo

Regular Expression: 10 digits with dash on position 3,4 or 5 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Im trying to verify an inputfield with regular expression. The user can enter a 10 digit number which can contain a dash on position 3,4 or 5. When i use only one expression, it works, but I can't get it to work with or-statements.
Offcourse it would be a solution to trim al the dashes, but i would like to do it this way.
Examples which should be allowed
0123456789
01-23456789
012-3456789
0123-456789
What I think should work
(^d{10}$)|
(^d{2}\-d{8}$)|
(^d{3}\-d{7}$)|
(^d{4}\-d{6}$)
You may use this regex with a lookahead and alternationn:
^(?:\d{10}|(?=[\d-]{11}$)\d{2,4}-\d+)$
RegEx Demo
RegEx Details:
^(?:: Start with a non-capturing group
\d{10}: Match all 10 digits
|: OR
(?=[\d-]{11}$): Make sure total length is 11 including -
\d{2,4}-\d+: Make sure - is at 3rd, 4th or 5th positions
)$: End non-capturing group in the end
As Revo suggested below a shorter version can be:
^(?=(?:-?\d){10}$)\d{2,4}-?\d+$
Try this pattern (?=\d{2,4}-\d{6,8})^[\d-]{11}$
It uses positive lookahead: (?=\d{2,4}-\d{6,8}) to assure that what follows is:
\d{2,4} - 2 up to 4 digits
- - dash literally
\d{6,8} - 6 to 8 digits
This assures that you will have dash at 2nd,3rd,4th position.
If this is satisfied, than match: ^ - beginning of a string, [\d-]{11} - eleven characters (digits or dash), $ - end of a string
Demo
Update: (?(?=\d{2,4}-\d{6,8})^[\d-]{11}$|^\d{10}$)
This uses alternation - if above lookahead is satisfied, it matches digits with dash, if i isn't, then it matches pattern ^\d{10}$, which is simply match ten digits only.
You just forgot to put backslashes in front of the d's. This regex should do it:
(^\d{10}$)|(^\d{2}\-\d{8}$)|(^\d{3}\-\d{7}$)|(^\d{4}\-\d{6}$)
Check out here: https://regex101.com/r/Nyjvfh/2

regex to validate strings with numbers and letters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need help to create a regex to validate one string, the string must be 12 characters, they can all be numbers or they can be 11 numbers and a letter, the letter can be anywhere between those numbers.
Some examples:
20710117540C
00A109822346
005109822346
K05109822346
// error cases
KY0510982234
KY05109822345
5505109822345
Thanks!
You can use this regex that will meet your requirements,
^(?:\d{12}|(?=\d*[a-zA-Z]\d*$)[\da-zA-Z]{12})$
Explanation:
^ - Start of string
(?: - Start of non-grouping pattern
\d{12} - Matches exactly 12 digits
| - alternation for another case where 11 characters can be any digits and one letter
(?=\d*[a-zA-Z]\d*$) - Look ahead to ensure the the incoming data consists of some digits and exactly one occurrence of alphabet
[\da-zA-Z]{12} - Consume 12 characters consisting of numbers and alphabet
)$ - End of non capturing group and end of input
Demo1
Another simple regex you can use is this,
^(?=\d*[a-zA-Z]?\d*$)[\da-zA-Z]{12}$
Explanation:
^ - Start of string
(?=\d*[a-zA-Z]?\d*$) - Look ahead ensuring the input contains some digits and either one alphabet or no alphabets.
[\da-zA-Z]{12} - Match and consume exactly 12 characters
$ - End of input.
Demo2

Regular Expression for specific count of letters and digits in a word. [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
In a file I have to fetch a string which contains exactly 5 letters followed by 5 numbers.
Some examples: TSDMN12345 TSDRD34526 PSSTN84586
Please help me how to match these.
Try:
if($str=~/^[a-zA-Z]{5}\d{5}$/){
...
}
Explanation:
^ - Beginning of a string
[a-zA-Z]{5} - exacly 5 english characters of upper or lower case
\d{5} - exacly 5 digits between 0 to 9
$ - end of a string
You need character classes (for "any letter" and "any number"), and then you need to specify how many occurrences to match.
/^
[[:alpha:]]{5} # 5 letters
\d{5} # 5 digits
$/x
(Note that the x on the end just allows us to use arbitrary whitespace to make our regex more legible)
Alternatively, based on your sample data you only care about upper case ASCII letters, in which case you can go for the much simpler
/^[A-Z]{5}\d{5}$/
The Perl documentation contains perlrequick and perlretut which I'd highly recommend.
You can match the string against:
"^[a-zA-Z]{5}\d{5}$"

Regex for example 22,123456 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Somebody know regex for two numbers, (comma) and then 6 numbers after comma. For example 22,123456. Somebody here who can help me?
This should match any decimal number:
/\d+,\d+/
Try this:
/\d+\.\d{6}/
/\d{2}\.\d{6}/
Or this if you wil use comma:
/\d+,\d{6}/
/\d{2},\d{6}/
Example: here
You may like this:-
\d{2},\d{6}
Explanation:-
\d{2} match a digit [0-9]
Quantifier: Exactly 2 times
, matches the character , literally
\d{6} match a digit [0-9]
Quantifier: Exactly 6 times
and two numbers before comma and six after:
/\d{2}\,\d{6}/
To limit strictly to 2 digits, a comma, 6 digits:
^\d\d,\d{6}$
where
^ : match the begining of the string<br>
\d\d : 2 digits
, : a comma
\d{6}: 6 digits
$ : end of the string
or, if the number is in the middle of a string:
\b\d\d,\d{6}\b
\b is a word boundary, it assumes there're no digits before and after.