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
Related
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
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 5 years ago.
Improve this question
I want to match a string where there are 5 characters where first four characters are A-Z and 5th is a digit. Also the first and the fourth character should be same.
I have a regex: [A-Z]{4}\d
However this wont check if 1st and 4th character are same. Please help
Regex: ^([A-Z])[A-Z]{2}\1\d$
1. ^ start of string.
2. ([A-Z]) capture first character.
3. [A-Z]{2} match next two character which can be in A-Z
4. \1 using captured group which contains first character of string.
5. \d a digit which can be 0-9
6. $ for end of string.
Regex demo
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 5 years ago.
Improve this question
I have a set of results that I would like to parse out using Regex and I can't seem to find an expression that works. On each line in a txt file, there are 2 entries each containing a quantity up to 100 followed by an item name of varying lengths and spaces.
Example:
7 BALLS OF STRING 13 CARDBOARD BOXES
14 ROCKS 12 PENCILS
I would like to match the 1st entry with the quantity in group 1, and the 2nd entry with it's quantity in group 2.
You can use the following regular expression pattern and use it while reading the file, line per line:
^(\d*\s[A-Z\s]*)\s(\d*\s[A-Z\s]*)$
Here is a live example: https://regex101.com/r/18dege/1
Here a few details:
^ matches the beginning of the string, $ the end of it
\d* matches any number (0 or more) of numeric characters greedy (equal to [0-9]*)
\s matches a white space character (e.g. tab, space, etc.)
[A-Z\s]* matches any number (0 or more) of uppercase characters and whitespace greedy
() creates a matching group (to extract some parts of the string)
According to the comment below, uppercase letters can be followed by lowercase letters, which should not be matched. An example for this would be:
7 BALLS OF STRING 13 CARDBOARD BOXES
14 ROCKS 12 PENCILS
18 TABLES 3 BLANKETS sewn with patches
To match this pattern, you can use the following regular expression:
^(\d*\s[A-Z\s]*?)[a-z\s]*\s(\d*\s[A-Z\s]*?)[a-z\s]*$
As an update to the above pattern, I've added the following:
[a-z\s]* between the statements (not in the group) and after the second statement, to match a lowercase string
(\d*\s[A-Z\s]*?) I've added a question mark ?, to make the matching non-greedy. This prevents adding the white space between the uppercase and the lowercase part to the matching group. It is now required to have an end of string character $ at the end of the pattern, otherwise, the second group would not match enough characters.
Here is a live example: https://regex101.com/r/18dege/2
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 8 years ago.
Improve this question
I have a string abcdefgh,
i want to check if the last two characters are alpha numeric/alphabets but not numeric
i.e. it can end in a1, 1a, aa but not 11
are there any regex gurus who can chipin
The regex should return the below results for the strings
abcd - True
abc1d - True
abcd1 - True
abc12 - false
This should do it:
^.*(\d[a-zA-Z]|[a-zA-Z](\d|[a-zA-Z]))$
Online regex tool demo here.
Meaning:
^ the beginning of the string
.* any character except \n (0 or more times)
(
\d[a-zA-Z] a digit (0-9) followed by any character of a to z, A to Z
| OR
[a-zA-Z] any character of a to z, A to Z followed by
(\d|[a-zA-Z]) a digit or any character of a to z, A to Z
)
$ end of the string
Notice this matches the whole string, not only the two last chars, having those at the matched group one.
So, a letter and a digit, or a digit and a letter, or two letters?
([a-zA-Z]\d|\d[a-zA-Z]|[a-zA-Z][a-zA-Z])$
Depending on the regex system you're using, you can also use character classes such as :alpha:, but this one will work for all of them. Some regex syntaxes need a backslash in front of the parentheses and/or pipe symbol.
You can use
^.*[A-Za-z].|.[A-Za-z]$
Online test
You can use :
^.*([a-zA-Z]{2}|(\d[a-zA-Z])|([a-zA-Z]\d))$
DEMO
EXPLANATION:
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