This question already has answers here:
Match exact string
(3 answers)
Closed 4 years ago.
Hi this is my example 0xc1e5017fbc68aa3e56aa580708de9aa123d62d18
This is my reg ex ^0x[a-fA-F0-9]{1,40}. What I'm doing is 0x which is compulsory after that we can enter upto 40 alpha numeric digits. But not special characters. What should I do here so it will match correctly?
Your issue is that you need to assert where the string ends, or else you will match up until a special character is found, and get undesired matches.
So with your current regex and test string 0xc1!, 0xc1 would be matched, even though it is an invalid match.
How about using: ^0x[a-fA-F0-9]{1,40}$
Related
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a string in the following format:
ABC12233434343DEF
How can I extract only:
ABC12233434343
I want to leave out the ending set of characters of whatever length they might be.
There are several ways this is one:
.*?\d+
It will match anything at the beginning that is followed by numbers.
It may be also posible to limit the characters it can match initially, like if you was capital letters from A-Z, for example:
[A-Z]+\d+
Online Demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have the following regex named 'pattern' that I am testing to try to restrict an input to only numbers. Why are both pattern.test("a") and pattern.test("1") returning true?
const pattern = /^[a-zA-Z0-9]*$/;
if (!pattern.test(event.target.value)) {
event.target.value = event.target.value.replace(/[^a-zA-Z0-9]/g, "");
}
Your expressions /^[a-zA-Z0-9]*$/ matches character ranges a-z, A-Z, 0-9. This will match any alphanumeric character. Adding the * means any number of times including zero. Meaning an empty string will also match.
If you want to match numeric and empty string only use /^[0-9]*$/.
If you want to match numeric only use /^[0-9]+$/.
This question already has answers here:
PHP: RegEx for string with 3-9 letters and 5-50 numbers
(2 answers)
Closed 5 years ago.
I want to match a string that has the following requirements:
The string contains 5 to 10 digits. [0-9]
Contains 2 to 3 letters. [a-z]
for example
123a2bc12
s123d456
This is my regular expression. /^\d*[a-z]\d*[a-z]\d*[a-z]?\d*$/. But this can only limit the number of letters.
It isn't pretty but this should be what you're looking for:
^(?=(?:[^a-zA-Z]*[a-zA-Z]){2,3}[0-9]*$)(?=(?:[^0-9]*[0-9]){5,10}[A-Za-z]*$).*$
I think you want to this
s=re.search(r'(\w+)','123a2bc12').group(1)
print s #The output is '123a2bc12'
This question already has answers here:
Regex how to match an optional character
(5 answers)
Closed 6 years ago.
I just want to write a regular expression 4 digits and '.' and 5 digits and optional 'A'
Ex: 1111.2345A where A is optional.
^[0-9]{4}[\.][0-9]{4}$
This reg ex will give 1111.2345, but how to add Optional 'N' at last.
Use ? at the end for characters:
[A-Za-z]?
This will match at most 1 presence of a character (lower or upper case).
You can check for a character zero or one times with this:
'[A]{0,1}'
Put that at the end of your string and it will try and match the character 'A' zero or one times. You may also use the symbol ? to match zero or one times. All about preference.
To get a single, optional A at the end, append A? to your regular expression:
^[0-9]{4}[\.][0-9]{4}A?$
Btw. instead of [0-9] you could use \d which stands for 'digit':
^\d{4}\.\d{4}A?$
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need to have a regular expression to restrict the number of digits in a string which may contain alphabets or any other characters along with digits. Is this possible with regular expressions ?
The idea is to anchor beginning and end of string and allow exactly n repetitions of a digit with something else. For example, exactly 6 digits in a string:
^\D*(\d\D*){6}$