trying to get a regex to match ranges - javascript

So far I have ^(\d.*[.]?\d*)|([<>][=]?\d.*[.]?\d*)|(\d.*[.]?\d*[-]\d.*[.]?\d*)$ but with my testing I get:
Should Match: (all do)
>1
<9
>=99
<=123123.134
1-2
44.421-234.123
123
123.123
0.123
Should not match: (examples marked with * are matching when they shouldn't)
123. *
.123
=<123 *
=>234 *
sdf
sdf.sdf
123.sdf *
Example here.

You can use something like this maybe:
^(?:[<>]?=?\d+(?:\.\d+)?|\d+(?:\.\d+)?-\d+(?:\.\d+)?)$
I trust that you already know how the anchors ^ and $ work.
(?: ... ) is a non-capture group, it's the same as a capture group ( ... ) except that it doesn't 'save' the matched parts. This is particular useful when you don't really need the matched part and you want to improve the efficiency, when compared to ( ... ) which takes a little overhead to store extra data.
[<>]?=? is for potential comparators.
\d+(?:\.\d+)? is used for numbers, integers or floating and ensure that there is no such thing as 123. or .123 since we are using the + quantifier on the \d.
So the regex matches a comparator followed by a number, or a subtraction between two numbers:
\d+(?:\.\d+)?-\d+(?:\.\d+)?
+-----------+|+-----------+
1st num | 2nd num
minus sign
regex101 demo

You can use this:
^(?:[<>]=?|\d+(?:.\d+)?-)?\d+(?:.\d+)?$

Related

Regex expression for serial numbers

I have the following specifications for a regex:
-> The string starts with a string of three numbers
-> It is followed by a '-'
-> That is followed by three uppercase vowels
-> That is followed by a '-'
-> That is followed by three numbers
-> That is followed by a final '-'
-> That is followed by the last three uppercase vowels.
-> Second set of numbers can not equal the first.
-> The second group of letters can not equal the first.
-> The groups of numbers may not contain zero.
A passable string is:
368-IOU-789-AIO.
An invalid string is:
368-AEO-368-AEI
354-AOU-431-AOU
Currently, I have something like this:
([0-9]+[0-9]+[0-9]+[/AEIOU/]+[0-9]+[0-9]+[0-9])
What you have won't work since + means "one or more of". For example, the sequence [0-9]+[0-9]+[0-9]+ will match anywhere between three and an infinite number of digits.
In addition, your current attempt:
allows for one to an infinite number of vowels (and possibly / character);
doesn't require a vowel set at the end;
doesn't require the - separators; and
may allow for arbitrary content before and after the match.
You should be able to use the {count} specifier to get an exact quantity. All but one of those limitations can be done with any basic regex engine, with something like:
^[1-9]{3}-[AEIOU]{3}-[1-9]{3}-[AEIOU]{3}$
The ^ and $ anchors means start and end of string, [1-9]{3} gives you exactly three non-zero digits, [AEIOU]{3} gives you exactly three vowels, and - gives you the literal separator character.
The "groups cannot be identical" rule is a little more problematic. I would just post process for that to ensure it's not violated. The following pseudo-code is what I mean:
def isValid(str):
if not str.regex_match("^[1-9]{3}-[AEIOU]{3}-[1-9]{3}-[AEIOU]{3}$"):
return false
return str[0..2] != str[8..10]
and str[4..6] != str[12..14]
The alternative will be a rather complex regex that future developers will probably curse you for inflicting on them :-)
Note that your "The groups of numbers may not contain zero" is a little ambiguous in that it may mean no zeros are allowed or just 000 is not allowed. I've assumed the former but it's easy adjustable to cater for the latter:
def isValid(str):
if not str.regex_match("^[0-9]{3}-[AEIOU]{3}-[0-9]{3}-[AEIOU]{3}$"):
return false
return str[0..2] != str[8..10]
and str[4..6] != str[12..14]
and str[0..2] != "000"
and str[8..10] != "000"
You can use capture group and backreferences
^([0-9]{3})-([AEIOU]{3})-(?:(?!\1)[0-9]){3}-(?:(?!\2)[AEIOU]){3}$
Regex Demo
The groups of numbers may not contain zero. From this if you meant only digits between 1 to 9 then you can replace [0-9] with [1-9]
If you don't want to have 000 then you can add a negative lookahead ^(?!.*000) to avoid matching 000

Match all number occurrences without any letter following or preceding

What I'm trying to accomplish:
Assuming an example string:
1 this is a1 my test 1a 12 string 12.123 whatever 1
I would like to have a Regex, that would give me all the occurrences of numbers (floats included), but I want it to skip the number if a letter (or more generally: non-number) precedes or follows it. So a1 and 1a would not match.
I've been struggling with this for a while, I got to this point (not ideal, because it also catches the preceding space):
/(^|\s)\d*\.*\d+/g
But this will also catch the 1a instance... I could also set up something similar, that would skip 1a, but would catch a1...
Can I accomplish this using regex matching?
You can use word boundaries with this regex:
/(?:\.\d+|\b\d+(?:\.\d+)?)\b/g
RegEx Demo
This is not a regex-only answer but maybe that's a good thing, we'll see.
The regex in use here is /^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/:
var testStr = '.1 this is a1 my test +5 1a 12 string -2.4 12.123 whatever . .02e1 5e5.4 1 1.4e5 1.2.3';
console.log('matches');
console.log(...testStr
.trim()
.split(/\s+/g)
.filter(word => /^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/
.test(word)
)
);
console.log('mismatches');
console.log(...testStr
.trim()
.split(/\s+/g)
.filter(word => !/^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/
.test(word)
)
);
For a simple, but not comprehensive solution (assuming numeric types used in the given example string, no negative numbers,scientific notation,etc.,), try this:
\d*\.*\d+
It removes the \s from your regex you developed, which matches the preceding space.
\d* will match all of the numbers (digits [0-9]).
Adding \.*\d+ will match floats (decimal followed by digits [0-9]).
Try this expression: (?<=^|\s)[-+]?(?:[0-9]*\.[0-9]+|[0-9]+)(?=$|\s) - Regex demo
JavaScript supported: (?:^|\s)([-+]?(?:[0-9]*\.[0-9]+|[0-9]+))(?=$|\s) - Regex demo
This expression supports floating-point numbers with an optional sign.
JS does not support positive lookbehind, so it was replaced by non-capturing group. The numbers are captured by the 1st group.

Regex to match only when expression match is no more than 12 characters long

I am trying to create a regular expression (Java/JavaScript) that matches the following regex, but only when there are fewer than 13 characters total (and a minimum of 4).
(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ DDD)?) ← originally posted
(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ [A-Z]+)?)
These values should (and do) match:
MED-123
COTA-1224
MED4
COTB-892K777
MED-33 DDD
MED-234J5678
This value matches, but I don't want it to (I want to only match if there are fewer than 12 characters total):
COT-1111J11111111111111
See http://regexr.com/3bs7b http://regexr.com/3bsfv
I have tried grouping my expression and putting {4,12} at the end, but that just makes it look for 4 to 12 instances of the whole expression matching.
I feel like I am missing something simple...thanks in advance for your help!
You can use negative look-ahead:
(?!.{13,})(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ DDD)?)
Since your expression already make sure that a match starts with COT or MED and there is at least one digit after that, it already guarantees that there are at least 4 characters
I have tried grouping my expression and putting {4,12} at the end, but
that just makes it look for 4 to 12 instances of the whole expression
matching.
This looks for 4 to 12 instances of the whole expression because you didn't add a word boundary \b. Your regex works fine, just add a word boundary and your desired outcome would be achieved. Take a look at this DEMO.
Your regex seems to be very clumsy and looks a little bit hard to read. It is also very limited to certain characters example JK except if you want it to be that way. For a more general pattern, you can check this out
(COT|MED)[AB]?-?[\dJK]{1,8}(\s+D{1,3})?\b
(COT|MED): matches either COT or MED
[AB]?: matches A or B which is optional because of the presence of ?
-?: matches - which is also optional
[\dJK]{1,8}: This matches a number,or J or K with a length of at least one character and a maximum of eight characters.
(\s+D{1,3})?: matches a space or a D at least one time and a maximum of 3 times and this is optional
\b: with respect to your question this seems to be the most important and it creates a boundary for the words that have already been matched. This means that anything exceeding the matched pattern would not be captured.
See the demo here DEMO2
The answer you are looking for is
(?!\S{13})(?:COT|MED)[ABCD]?-?\d{1,4}(?:[JK]+\d*|(?: [A-Z]+)?)
See regex demo
Note that it is almost impossible to check the length of a phrase that is not a whole string or that has spaces inside since boundaries are a bit "blurred". Thus, (?!\S{13}) is a kind of a workaround that just makes sure you do not have a string without whitespace that is 13 characters long or longer.
The regex breakdown:
(?!\S{13}) - Check if the substring that follows does not consist of 13 non-whitespace characters
(?:COT|MED) - Any of the values in the alternation (COTorMED`)
[ABCD]?-? - Optional A, B, C, D and then an optional -
\d{1,4} - 1 to 4 digits
(?:[JK]+\d*|(?: [A-Z]+)?) - a group of 2 alternatives:
[JK]+\d* - J or K, 1 or more times, and then 0 or more digits
(?: [A-Z]+)? - optional space and 1 or more Latin uppercase letters
As this answer suggests, you could solve this this way:
(?=(COT|MED)[ABCD]?-?[0-9]{1,4}(([JK]+[0-9]*)|(\ DDD)?))(?={4 , 12})

Regex Javascript Phone number validation min max length check

I want a very simple Regex for Javascript validation of phone number, which allows 10 digits and checks min numbers should be 10 and max 12 including - dash two times for ex. 123-123-1234
I have found some on internet, but none of them is working for min / max length.
Looking forward for quick response here.
Thanks !
You could do this
/^(?!.*-.*-.*-)(?=(?:\d{8,10}$)|(?:(?=.{9,11}$)[^-]*-[^-]*$)|(?:(?=.{10,12}$)[^-]*-[^-]*-[^-]*$) )[\d-]+$/
See it here on Regexr
(?!...) is a negative lookahead assertion
(?=...) is a positive lookahead assertion
^ # Start of the string
(?!.*-.*-.*-) # Fails if there are more than 2 dashes
(?=(?:\d{8,10}$) # if only digits to the end, then length 8 to 10
|(?:(?=.{9,11}$)[^-]*-[^-]*$) # if one dash, then length from 9 to 11
|(?:(?=.{10,12}$)
[^-]*-[^-]*-[^-]*$ # if two dashes, then length from 10 to 12
)
)
[\d-]+ # match digits and dashes (your rules are done by the assertions)
$ # the end of the string
What you asking for wouldn't be a simple regular expression and also may be solved without any use 'em.
function isPhoneNumberValid(number){
var parts, len = (
parts = /^\d[\d-]+\d$/g.test(number) && number.split('-'),
parts.length==3 && parts.join('').length
);
return (len>=10 && len<=12)
}
For sure this may be a bit slower than using a compiled regex, but overhead is way minor if you not going to check hundreds of thousandths phone numbers this way.
This isn't perfect in any way but may fit your needs, beware however that this allow two dashes in any place excluding start and end of number, so this will return true for string like 111--123123.
There's no simple way to do this with regexp, especially if you allow dashes to appear at some different points.
If you allow dashes only at places as in your example, then it would be ^\d{3}-?\d{3}-?\d{4}$
This one: ^[\d-]{10,12}$ matches string with length from 10 to 12 and contains only digits and dashes, but it also will match e.g. -1234567890-.

European Numbers RegEx

I'm using this /[-\+,\.0-9]+/ to match numbers in strings like +4400,00 % or -3500,00 % or 0.00 %.
The matched results I want is +4400,00 and I correctly get it.
What if I wanted the same results for a string like +4.400,00 % (dot for thousands) ?
EDIT
How do I have to modify my RegEx for matching numbers in strings like <font color="red">+44.500 %</font>?
/[\-\+]?\s*[0-9]{1,3}(\.[0-9]{3})*,[0-9]+/
That should cover strings that
may start with a + or -, and then perhaps some whitespaces
then have between one and three numbers
then have groups of three numbers, prefixed with a period
then have a comma and at least one number behind the comma
Regarding your additional question (matching numbers inside strings), you should look into the manual of whatever regex API you're using. Most APIs have separate search and match methods; match wants the whole string to be part of your regular expression's language, while search will also match substrings.
[\+-]? - plus or minus
\d{1,3} - some digits
(\.\d{3})* - groups of 3 digits with point before
,\d{2} comma and 2 more digits
And so we get:
/[+-]?\d{1,3}(\.\d{3})*,\d{2}/
Your regex will already match ".". But it sounds like you also want to strip "." out? if that's the case, you need a substiution. In Perl,
if ($input =~ /(-|\+)[0-9][,\.0-9]+/) {
$input =~ s/\.//;
} else {
die;
}
I've also changed the regex so it will only match - and + at the start, and so it requires an initial digit

Categories