I have a regex
/^([a-zA-Z0-9]+)$/
this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.
Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:
/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/
This RE will do:
/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i
Explanation of RE:
Match either of the following:
At least one number, then one letter or
At least one letter, then one number plus
Any remaining numbers and letters
(?:...) creates an unreferenced group
/i is the ignore-case flag, so that a-z == a-zA-Z.
I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.
An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".
So, test for this :-
/^([a-zA-Z0-9]+)$/
Then this :-
/\d/
Then this :-
/[A-Z]/i
If your string passes all three regexes, you have the answer you need.
The accepted answers is not worked as it is not allow to enter special characters.
Its worked perfect for me.
^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$
one digit must
one character must (lower or upper)
every other things optional
Thank you.
While the accepted answer is correct, I find this regex a lot easier to read:
REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"
This solution accepts at least 1 number and at least 1 character:
[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))
And an idea with a negative check.
/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
^(?! at start look ahead if string does not
\d*$ contain only digits | or
[a-z]*$ contain only letters
[a-z\d]+$ matches one or more letters or digits until $ end.
Have a look at this regex101 demo
(the i flag turns on caseless matching: a-z matches a-zA-Z)
Maybe a bit late, but this is my RE:
/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/
Explanation:
\w* -> 0 or more alphanumeric digits, at the beginning
\d+[a-zA-Z]|[a-zA-Z]+\d -> a digit + a letter OR a letter + a digit
\w* -> 0 or more alphanumeric digits, again
I hope it was understandable
What about simply:
/[0-9][a-zA-Z]|[a-zA-Z][0-9]/
Worked like a charm for me...
Edit following comments:
Well, some shortsighting of my own late at night: apologies for the inconvenience...
The - incomplete - underlying idea was that only one "transition" from a digit to an alpha or from an alpha to a digit was needed somewhere to answer the question.
But next regex should do the job for a string only comprised of alphanumeric characters:
/^[0-9a-zA-Z]*([0-9][a-zA-Z]|[a-zA-Z][0-9])[0-9a-zA-Z]*$/
which in Javascript can be furthermore simplified as:
/^[0-9a-z]*([0-9][a-z]|[a-z][0-9])[0-9a-z]*$/i
In IMHO it's more straigthforward to read and understand than some other answers (no backtraking and the like).
Hope this helps.
If you need the digit to be at the end of any word, this worked for me:
/\b([a-zA-Z]+[0-9]+)\b/g
\b word boundary
[a-zA-Z] any letter
[0-9] any number
"+" unlimited search (show all results)
Related
I would like to find the match for:
\024jack3hall2\c$
\024jack3hall02\c$
\024jack3hall12\c$
but not for:
\024jack3hall023\c$
difference is the number of digits in the end part. I would like to have only 1 or 2, not 3.
my try:
\\\\024[a-zA-Z0-9]+[0-9]{1,2}\\[a-zA-Z]{1}\$(?!.)
I tried only on http://regexr.com/ but will implement in C#.
Is it possible to edit my try or I have to write several separate checks?
Why is
{1,2}
not working? \024jack3hall12343\c$ is also matching,
From the examples you have shown, something as simple as:
[^\d](\d{1,2})\\
Should work. It will match 1 or 2 digits followed by a \ so long as it isn't proceeded by another digit.
The matched digits are in a capture group if you need them (or you can just remove the brackets if you don't need that).
As for your original effort, right here:
\\\\024[a-zA-Z0-9]+[0-9]{1,2}
You are matching 1 or more from the range a-z, A-Z or 0-9. So that will match your extra digits if they come at the end of that pattern.
Answer:
\\\\024[a-zA-Z0-9]+[^\d](\d{1,2})\\[a-zA-Z]{1}\$(?!.)
I believe you were not escaping backslash properly.
Here is the correct regex:
\\024[a-zA-Z0-9]+[0-9]{1,2}\\[a-zA-Z]{1}\$(?!.)
Can someone please validate this for me (newbie of regex match cons).
Rather than asking the question, I am writing this:
Regex rgx = new Regex (#"^{3}[a-zA-Z0-9](\d{5})|{3}[a-zA-Z0-9](\d{9})$"
Can someone telll me if it's OK...
The accounts I am trying to match are either of:
1. BAA89345 (8 chars)
2. 12345678 (8 chars)
3. 123456789112 (12 chars)
Thanks in advance.
You can use a Regex tester. Plenty of free ones online. My Regex Tester is my current favorite.
Is the value with 3 characters then followed by digits always starting with three... can it start with less than or more than three. What are these mins and max chars prior to the digits if they can be.
You need to place your quantifiers after the characters they are supposed to quantify. Also, character classes need to be wrapped in square brackets. This should work:
#"^(?:[a-zA-Z0-9]{3}|\d{3}\d{4})\d{5}$"
There are several good, automated regex testers out there. You may want to check out regexpal.
Although that may be a perfectly valid match, I would suggest rewriting it as:
^([a-zA-Z]{3}\d{5}|\d{8}|\d{12})$
which requires the string to match one of:
[a-zA-Z]{3}\d{5} three alpha and five numbers
\d{8} 8 digits or
\d{12} twelve digits.
Makes it easier to read, too...
I'm not 100% on your objective, but there are a few problems I can see right off the bat.
When you list the acceptable characters to match, like with a-zA-Z0-9, you need to put it inside brackets, like [a-zA-Z0-9] Using a ^ at the beginning will negate the contained characters, e.g. `[^a-zA-Z0-9]
Word characters can be matched like \w, which is equivalent to [a-zA-Z0-9_].
Quantifiers need to appear at the end of the match expression. So, instead of {3}[a-zA-Z0-9], you would need to write [a-zA-Z0-9]{3} (assuming you want to match three instances of a character that matches [a-zA-Z0-9]
In detail, needs to match a string with the following pattern:
Always starts with plain English letters (i.e. A-Z, a-z)
The length of the above letter is between 1 to 3
Followed by any numbers of which the first digit is not 0 (i.e. AB01 is not valid but AB1 is)
Thanks.
How about:
[A-Za-z]{1,3}[1-9]\d*
which is a literal translation of your remarkably precise question.
Cameron is right, except you have to put in a starting limit. For future refernce, you should look into a good online regex tester. I like Derek Slager's.
^[A-Za-z]{1,3}[1-9]\d*
if you want to match the whole line, and not just the beginning, then
^[A-Za-z]{1,3}[1-9]\d*$
Hi all i need a regex that accepts first letter as character and the remaining should be digits.
Even spacing is not allowed too..
Possible cases : a123, abc123, xyz123 and so on ...
Unacceptable : 123abc,1abc12, a 123 and so on..
i tried some think like this but i am not sure this works so can any one help me..
"[A-Z][a-z]\d{0,9}"
^[A-Za-z]+[0-9]+$
matches one or more ASCII letters, followed by one or more ASCII digits. If digits are optional, use [0-9]* instead.
If you want to also allow other letters/digits than just ASCII, use
^\p{L}+\p{D}+$
you probably need this:
"[a-zA-Z]+\d+"
How about this expression [A-Za-z]\w*
[A-Z]|[a-z]{1,}\d{1,}
But as you mentioned, possible cases should be: a123, b321, z4213213, but not abc123. Right?
So regExp shoud be [A-Z]|[a-z]\d{1,} .
I would highly recommend you do not limit yourself to ASCII as most of the other answers for this question do.
Using character classes, which I recommend, you should use:
^[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Pc}]\d+$
See the reference for ^ and $.
This is something of a follow up from a previous question. The requirements have changed, and I'm looking for some help with coming up with regex for either a comma separated number or a number with no commas. Sample input is below, along with some failed attempts.
Any help is appreciated.
Acceptable input:
1,234,567
1234567
Unacceptable input:
1234,567
1,234567
Some failed attempts:
^(\d{1,3}(?:[,]\d{3})*)|((\d)*)$
^\d{1,3}((?:[,]?\d{3})*|(?:[,]\d{3})*)$
Original success:
^\d{1,3}(?:[,]\d{3})*$
Just add an "or one or more digits" to the end:
^(?:\d{1,3}(?:[,]\d{3})*|\d+)$
I think you had it almost right the first time and just didn't match up all your parentheses correctly.
Try this:
^\d{1,3}(?:(?:,\d{3})+|\d*)$
This will match any sequence that begins with one to three digits, followed by either
one or more segments of a comma followed by three digits, or
zero or more digits.
Why not use Int.TryParse() rathern then a regex?
Please see this answer for a definitive treatment of the “Is it a number?” question, including allowance for correctly comma-separated digit groups.
One thing i've noticed with all these is that the first bit of the regex allows for '0' based numbers to work. For example the number:
0,123,456
Would match using the accepted answer. I've been using:
((?<!\w)[+-]?[1-9][0-9]{,2}(?:,[0-9]{3})+)
Which also ensures that the number has nothing in front of it. It does not catch numbers of less then 1,000 however. This was to prevent ill-formatted numbers from being captured at all. If the final + were a ? the following numbers would be captured:
0,123
1,2 (as 2 separate numbers)
I have a strange set of numbers to match for (integers with commas, with spaces and without both), so i'm using pretty restrictive regexs to capture these groups.
Anyway, something to think about!