I am trying to match an input of the following format
[Number]-[Number] using regex in JavaScript.
For ex :
1-100 OK
200-300 OK
-0992 NOT OK
aa-76 NOT OK
1- NOT OK
I tried:
^\d+(-\d+)*$
But this does not work at all.
Any pointers?
The reason it doesn't work is that the * quantifier makes the (-\d+) optional, allowing the regex to simply match 222. Remove it and all will be well. If you don't need the parentheses, strip them:
^\d+-\d+$
What if you want your numbers to be between 0 and 100 as in title?
If you want to make sure that your numbers on each side are in the range from 1 to 100, without trailing zeros, you can use this instead of \d:
100|[1-9]\d|\d
Your regex would then become:
^(?:100|[1-9]\d|\d)-(?:100|[1-9]\d|\d)$
What if the left number must be lower than the right number?
The regexes above will accept 2222-1111 for the first, 99-12 for the second (for instance). If you want the right number to be greater than the lower one, you can capture each number with capturing parentheses:
^(\d+)-(\d+)$
or
^(100|[1-9]\d|\d)-(100|[1-9]\d|\d)$
Then, if there is a match, say
if(regexMatcher.group(2) > regexMatcher.group(1)) { ... success ...}
The regex you are looking for is /\d+-\d+/. If you don't require the whole line to match the regex, then there is no need for surrounding ^ and $. For instance:
/\d+-\d+/.test("a-100")
// Result: false
/\d+-\d+/.test("-100")
// Result: false
/\d+-\d+/.test("10-100")
// Result: true
Related
I have a filename that will be something along the lines of this:
Annual-GDS-Valuation-30th-Dec-2016-082564K.docx
It will contain 5 numbers followed by a single letter, but it may be in a different position in the file name. The leading zero may or may not be there, but it is not required.
This is the code I come up with after checking examples, however SelectedFileClientID is always null
var SelectedFileClientID = files.match(/^d{5}\[a-zA-Z]{1}$/);
I'm not sure what is it I am doing wrong.
Edit:
The 0 has nothing to do with the code I am trying to extract. It may or may not be there, and it could even be a completely different character, or more than one, but has nothing to do with it at all. The client has decided they want to put additional characters there.
There are at least 3 issues with your regex: 1) the pattern is enclosed with anchors, and thus requires a full string match, 2) the d matches a letter d, not a digit, you need \d to match a digit, 3) a \[ matches a literal [, so the character class is ruined.
Use
/\d{5}[a-zA-Z]/
Details:
\d{5} - 5 digits
[a-zA-Z] - an ASCII letter
JS demo:
var s = 'Annual-GDS-Valuation-30th-Dec-2016-082564K.docx';
var m = s.match(/\d{5}[a-zA-Z]/);
console.log(m[0]);
All right, there are a few things wrong...
var matches = files.match(/\-0?(\d{5}[a-zA-Z])\.[a-z]{3,}$/);
var SelectedFileClientID = matches ? matches[1] : '';
So:
First, I get the matches on your string -- .match()
Then, your file name will not start with the digits - so drop the ^
You had forgotten the backslash for digits: \d
Do not backslash your square bracket - it's here used as a regular expression token
no need for the {1} for your letters: the square bracket content is enough as it will match one, and only one letter.
Hope this helps!
Try this pattern , \d{5}[a-zA-Z]
Try - 0?\d{5}[azA-Z]
As you mentioned 0 may or may not be there. so 0? will take that into account.
Alternatively it can be done like this. which can match any random character.
(\w+|\W+|\d+)?\d{5}[azA-Z]
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 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)
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$
Max Length of string is 5 (including one alphabet). If there is no alphabet, allowed length of digits is 4.
Digits allowed: 0 to 9999
One alphabet is allowed(Only if string has at least one number). Some examples:
Allowed: 1a, a2, 1111a, 1a22, 9999
Not allowed: 99999, 11111,a,aa
I tried:
^(?:[0-9]|[a-z](?=[^a-z]*$)){1,5}$
This works for cases: 1a, a2, 1111a, 1a22, 9999. But it incorrectly allows 99999 as well.
Any help on how to restrict the digit length?
^(?:(?=\d*[a-z]\d*$)(?=.*[0-9])(?:[a-z0-9]){1,5}|[0-9]{1,4})$
Try this.See demo.
https://regex101.com/r/fX3oF6/10
Regexes aren't good at keeping counts of things, as you've discovered. In this case, a lookahead will put you right:
^\d{1,4}$|^(?=\d*[a-z]\d*$)[a-z\d]{1,5}$
We start by using ^\d{1,4}$ to get the simplest case out of the way first. If that fails, the second alternative, the second alternative takes over. The first thing it does is use (?=\d*[a-z]\d*$) to assert that there is exactly one letter in the string. If the lookahead succeeds, the match position returns to the beginning of the string, allowing us match the whole string again, this time with [a-z\d]{1,5}$.
It isn't really necessary to verify that the rest of the characters are digits at this point. I could have used (?=[^a-z]*[a-z][a-z]*$ instead. We just need to make sure it looks at the whole string. I just think it's more self-documenting with \d*.
Note that this regex will match a string consisting of just a letter. If you want to make sure there's at least one digit as well, change the final {1,5} to {2,5}.
Here's the demo.
Use {size} for restrict the length of String in regex.
I update the regex:
^(?:(?=.*[a-z])(?:[0-9]|[a-z]){1,5}|[0-9]{4})$