validating last two characters to follow a pattern [closed] - regex

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:

Related

regex for certain characters and rules [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
I am trying to build a regex to ensure a given string only contains these 13 certain characters/rules. I am having a bit of trouble. Any help would be appreciated.
Allowed Characters:
a-z
A-Z
0-9
! (Exclamation point)
- (Hyphen)
_ (Underscore)
. (Period)
* (Asterisk)
' (Single Quote)
( (Open parenthesis)
) (Close parenthesis)
(No consecutive spaces)
*. (CANNOT end with a period)
So Far I have this
/^[+\-0-9().!-_*' ]+$/g
But not getting expected results. Thank you in advance.
EDIT:
Sorry first time posting here. Here are some test cases(JS). Second one should not pass because it has consecutive spaces and ends with period.:
let testOne = "Testing Regex - 2021 Th*i)s_On(e_pa!'ss.es.end";
let testTwo ="Testing Regex - 2021 Th*i)s_On(e_pa!'ss.es.end but
shouldn't.";
testOne.match(/^[+\-\w().!-_*' ]+$/g);
testTwo.match(/^[+\-\w().!-_*' ]+$/g);
Some issues:
Your regex does not allow for Latin letters: you didn't list them.
Your regex allows for some additional characters (including $, # and %) because of !-*, which specifies a range.
There is no provision for not allowing more than a single space.
There is no provision for not allowing a dot as last character
The g modifier has little purpose when you have end-of-string markers requiring that a match will always match the whole input.
From your regular expression it seems you also require that the input has at least 1 character.
Taken all that together, we get this:
/^(?!.*? )(?!.*?\.$)[\w+\-().!*' ]+$/
You can try this:
^(?!.* )[\w!()\-*'\s.]+[\w!()\-*'\s]$
https://regex101.com/r/kTcJUN/3
And if you don't want to allow space character at the end of string then:
^(?!.* )[\w!()\-*'\s.]+[\w!()\-*']$
Explanation:
(?!.* ) - Exclude double space in string
\w - any word character. Matches any letter, digit or underscore. Equivalent to [a-zA-Z0-9_].
! - literally !
( - literally (
) - literally )
- - literally -
* - literally *
' - literally '
\s - space character
. - literally .
+ - quantifier. Matches between one and unlimited times.
[\w!()\-*'\s] - Allow a single character from the list. Putting this just before $ (end of line) makes this character last in string.

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

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

Using captured groups in Regex [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 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

Filter lines based on range of value, using regex [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 6 years ago.
Improve this question
What regex will work to match only certain rows which have a value range (e.g. 20-25 days) in the text raw data (sample below):
[product-1][arbitrary-text][expiry-17days]
[product-2][arbitrary-text][expiry-22days]
[product-3][arbitrary-text][expiry-29days]
[product-4][arbitrary-text][expiry-25days]
[product-5][arbitrary-text][expiry-10days]
[product-6][arbitrary-text][expiry-12days]
[product-7][arbitrary-text][expiry-20days]
[product-8][arbitrary-text][expiry-26days]
'product' and 'expiry' text is static (doesn't change), while their corresponding values change.
'arbitrary-text' is also different for each line/product. So in the sample above, the regex should only match/return lines which have the expiry between 20-25 days.
Expected regex matches:
[product-2][arbitrary-text][expiry-22days]
[product-4][arbitrary-text][expiry-25days]
[product-7][arbitrary-text][expiry-20days]
Thanks.
Please check the following regex:
/(.*-2[0-5]days\]$)/gm
( # start capturing group
.* # matches any character (except newline)
- # matches hyphen character literally
2 # matches digit 2 literally
[0-5] # matches any digit between 0 to 5
days # matches the character days literally
\] # matches the character ] literally
$ # assert position at end of a line
) # end of the capturing group
Do note the use of -2[0-5]days to make sure that it doesn't match:
[product-7][arbitrary-text][expiry-222days] # won't match this
tested this one and it works as expected:
/[2-2]+[0-5]/g
[2-2] will match a number between 2 and 2 .. to restrict going pass the 20es range.
[0-5] second number needs to be between 0 and 5 "the second digit"
{2} limit to 2 digits.
Edit : to match the entire line char for char , this shoudl do it for you.
\[\w*\-\d*\]\s*\[\w*\-[2-2]+[0-5]\w*\]
Edit2: updated to account for Arbitrary text ...
\[(\w*-\d*)\]+\s*\[(\w*\-\w*)\]\s*\[(\w*\-[2-2]+[0-5]\w*)\]
edit3: Updated to match any character for the arbitrary-text.
\[(\w*-\d*)\]\s*\[(.*)\]\s*\[(\w*\-[2-2][0-5]\w*)\]
.*\D2[0-5]d.*
.* matches everything.
\D prevents numbers like 123 and 222 from being valid matches.
2[0-5] covers the range.
d so it doesn't match the product number.
I pasted your sample text into http://regexr.com
It's a useful tool for building regular expressions.
You can try this one :
/(.*-2[0-5]days\]$)/gm
try it HERE