I have been trying to make a regex that will: match only if all condition are met, will not match 1 to 10 or something like it and will ignore commas.
I have made (?=.*1)(?=.*5) which almost works and will match to 1,5 as it is meant to but will also match to 10,5,10,50 and 1,50. I can not work out how to stop this. So my question is how do I get the regex to know the difference between numerals and a single digit.
Make it match word boundaries around the number, using \b.
(?=.*\b1\b)(?=.*\b5\b)
Related
I need to write a little RegEx matcher which will match any occurrence of strings in the form of
[a-zA-Z]+(_[a-zA-Z0-9]+)?
If I use the regex above it does match the sections needed but would also match onto the abc part of 4_abc which is not intended. I tried to exclude it with:
(?:[^a-zA-Z0-9_]|^)([a-zA-Z]+(_[a-zA-Z0-9]+)?)(?:[^a-zA-Z0-9_]|$)
The problem is that the 'not' matches at the beginning and end are not really working like I hoped they would. If I use them on the example
a_d Dd_da 4_d d_4
they would block matching the second Dd_da because the space was used in the first match.Sadly I can't use lookarounds because I am using JS.
So the input:
a_d Dd_da 4_d d_4
should match: a_d, Dd_da and d_4
but matches: a_d (there is a space at the end)
Is there another way to match the needed sections, or to not consume the 'anchor' matches?
I really appreciate your help.
You can make use of \b:
\b[a-zA-Z]+(_[a-zA-Z0-9]+)?\b
\b matches the (zero-width) point where either the preceding character or following character is a letter, digit or underscore, but not both. It also matches with the start/end of the string if the first/last character is a letter, digit or underscore.
I have to check for capital letters to exist just at the beginning of words.
My regex now looks like this:
/^([A-ZÁÉÚŐÓÜÖÍ]([a-záéúőóüöí]*\s?))+$/
It's at the words beginning works good, but if the problem not at the beginning of the word it's fails.
For example: John JohnJ got validated.
What should i alternate in my regex to works well?
In your regex pattern the space is optional, allowing combinations like JJohn or JohnJ - the key is to make it required between words. There are two ways to do this:
Roll out your pattern:
/^[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*(?:\s[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*)*$/
Or make the space in your pattern required, but alternatively allow it to be the end of line (this allows a trailing space though).
/^(?:[A-ZÁÉÚŐÓÜÖÍ][a-záéúőóüöí]*(?:\s|$))+$/
In both patterns I have removed some superfluous groups of your original and turned all groups into non-capturing ones.
You can do this: /^([A-ZÁÉÚŐÓÜÖÍ]{0,1}([a-záéúőóüöí]*\s?))+$/
With {a,b}, a is the least amount of characters it will match, whereas b is the most amount of characters it will match.
If there is ALWAYS going to be a capital letter at the beginning, instead you can simply use: /^([A-ZÁÉÚŐÓÜÖÍ]{1}([a-záéúőóüöí]*\s?))+$/
In this preceding case, {c}, c is the exact number of characters it will match.
Here is a resource with good information.
I've written a regular expression that matches any number of letters with any number of single spaces between the letters. I would like that regular expression to also enforce a minimum and maximum number of characters, but I'm not sure how to do that (or if it's possible).
My regular expression is:
[A-Za-z](\s?[A-Za-z])+
I realized it was only matching two sets of letters surrounding a single space, so I modified it slightly to fix that. The original question is still the same though.
Is there a way to enforce a minimum of three characters and a maximum of 30?
Yes
Just like + means one or more you can use {3,30} to match between 3 and 30
For example [a-z]{3,30} matches between 3 and 30 lowercase alphabet letters
From the documentation of the Pattern class
X{n,m} X, at least n but not more than m times
In your case, matching 3-30 letters followed by spaces could be accomplished with:
([a-zA-Z]\s){3,30}
If you require trailing whitespace, if you don't you can use: (2-29 times letter+space, then letter)
([a-zA-Z]\s){2,29}[a-zA-Z]
If you'd like whitespaces to count as characters you need to divide that number by 2 to get
([a-zA-Z]\s){1,14}[a-zA-Z]
You can add \s? to that last one if the trailing whitespace is optional. These were all tested on RegexPlanet
If you'd like the entire string altogether to be between 3 and 30 characters you can use lookaheads adding (?=^.{3,30}$) at the beginning of the RegExp and removing the other size limitations
All that said, in all honestly I'd probably just test the String's .length property. It's more readable.
This is what you are looking for
^[a-zA-Z](\s?[a-zA-Z]){2,29}$
^ is the start of string
$ is the end of string
(\s?[a-zA-Z]){2,29} would match (\s?[a-zA-Z]) 2 to 29 times..
Actually Benjamin's answer will lead to the complete solution to the OP's question.
Using lookaheads it is possible to restrict the total number of characters AND restrict the match to a set combination of letters and (optional) single spaces.
The regex that solves the entire problem would become
(?=^.{3,30}$)^([A-Za-z][\s]?)+$
This will match AAA, A A and also fail to match AA A since there are two consecutive spaces.
I tested this at http://regexpal.com/ and it does the trick.
You should use
[a-zA-Z ]{20}
[For allowed characters]{for limiting of the number of characters}
I am having trouble with a negative lookahead in JavaScript. This may seem like a silly thing to regex, but it's still something I'm trying to figure out.
Say I have the following string:
>>a+b+c
and I know that the first two characters (a,b) can be in any order, but the last character (c) must be last. The characters cannot be repeated, and all characters must be separated by a plus (+).
I am using a negative lookahead in the following manner:
^>>(?:([ab+])(?!.*\1))*$
Testing the following strings works until this point:
>>a+b //true, as expected
>>b+a //true, as expected
However, when I try to move beyond the negative lookahead, I can't seem to get things to work:
^>>(?:([ab+])(?!.*\1))*\+c$
Test the following strings:
>>a+b+c //false, expecting true
>>b+a+c //false, expecting true
What am I not understanding?
Your problem is that your capture matches the single + in the pattern which then cannot be repeated (that is, the repetition of + in your input makes the match fail).
Try this regex instead:
^>>(?:([ab])\+(?!.*\1))*c$
Try adding a . before your last *. You want to match characters after doing the lookahead check, not match 0 or more of the whole check. (Assuming you only want a+b+c or b+a+c to match, although if those are the only to combos, you might just literally match those two with an or.)
^>>(?:([ab+])(?!.*\1)).*\+c$
I believe that will also match just a+c so this might be what you want instead:
^>>(?:([ab])\+(?!\1)).\+c$
I know how to do a regex to validate if it's just letter number without no white spaces:
/^[0-9a-zA-Z]+$/
but how do I add to this regex also such that it cannot contain just numbers, so for example this is not valid:
08128912382
Any ideas?
"Must contain only letters and numbers and at least one letter" is equivalent to "must contain a letter surrounded by numbers or letters":
/^[0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*$/
I would like to add that this answer shows a way you can think about the problem so writing the regexp is simpler. It is not meant to be the best solution to the problem. I just took what you had and gave it a nudge in the right direction.
With several more nudges, you end up with other different answers (posted by ZER0, Tomalak and OGHaza respectively) :
You could notice that if there is a letter in the first or last group, the middle part is satisfied. In other words, since you have the middle part, you don't need to allow letters in the first or last part (but not both!):
/^[0-9]*[a-zA-Z][0-9a-zA-Z]*$/ - some numbers, followed by a letter, followed by some more numbers and letters
/^[0-9a-zA-Z]*[a-zA-Z][0-9]*$/ - equivalent if you read from the end
Knowing about lookaheads you can assert that there is at least one letter in the string:
/^(?=.*[a-z])/ - matches the start of any string that contains at least 1 letter
Or the other way around, as you expressed it, assert that there aren't only numbers in the string:
/^(?!\d+$)/ - matches the start of any string which doesn't contain just digits
The 2nd and 3rd solutions should also be combined with your original regexp that validates that the string contains only the characters you want it to (letters and numbers)
I for one am particularly fond of the 2nd solution which is i believe the fastest of all attempted so far.
A look-ahead can do it:
/^(?=.*[a-z])[0-9a-z]+$/i
I think the most elegant solution is a negative lookahead to check it's not only numbers
/^(?!\d+$)[0-9a-zA-Z]+$/
RegExr Example
So basically you need at that at least one letter is in the string. In that case you can just check the presence of one or more letter, preceded maybe by one or more numbers, and maybe followed by both:
/^[0-9]*[a-z][0-9a-z]*$/i
Notice that it will returns true if you test against string like "A" for instance, because in this case all the numbers are considered optional.