Feet inch regex of "universal" combinations - javascript

So, there are plenty of questions and answers about standard ways of parsing feet and inches with regex. A casual search led me easily to:
Link1
Link2
But these options are very specific with what they are looking for and are not versatile enough for what I would want an expression to do. Desirably, I wanted a regex to match all ft inches dimensions like those below:
Example matches (12 and 3 and 1/4 are just examples, matches should not be by line (not just $)):
12' 3"
12 feet 3 inches
12 ft 3 in
12 ft. 3 in.
12' -3"
-12 feet 3 inches (Should capture the negatives)
12'- 3" (Should not mistaken as -ve 3, but not necessarily needed to be processed with regex)
12' 3 1/4"
12' 3.25"
12 ft 3 1/4 in.
12' (Need to capture "single" dimensions provided that it is not logically matched to next or prior)
3"
3 1/4 inches
-3.25"
3 1/4feet
Desired non-matches
12' 12'
3inches 12'
3 inches 3 inches (need to match separately = matches 3 inches twice)
3 - 2ft (need to be able to exclude the 3 and only match -2ft)
I started out trying to write something and came up with:
/(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)/gi
But it's too greedy and would accept 12' 12' as one thing. So I started doing some exclusions like what they did within Link 1 up there, but I couldn't get it such that it will work. I tried this:
(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)(?!=(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|')+(?:[ -]*)){1,2}
and also
((?<!((-*[\d .]+(\/\d)* *){1}(?:INCH|INCHES|IN\.*|")+)))(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|'|INCH|INCHES|IN\.*|")+(?:[ -]*)(?!=(-*[\d .]+(\/\d)* *){1}(?:FEET|FT\.*|')+(?:[ -]*)){1,2}
and I have tried some other approaches, such as
(([-*\d+ *])+(?:FEET|FT\.*|')+(?:\s*-)*){0,1}((\s*\d+[./]*\d*\s*)+(?:INCH|INCHES|IN\.*|")+(?: )+){0,1}
and it works the way I desired, but it also matches a lot of empty strings.
Maybe I am just not looking hard enough or searching the right terms, but I don't think I came across an old post that has something as versatile as I would have liked. If there has been an answer previously that does exactly what I would like, feel free to point it out for me. Thanks!

I came up with this regex based on the test cases provided:
/(?:-[ \t]*)?((?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:[']|feet|ft\.?)(?:[ \t]*(?:-[ \t]*)?(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:["]|inch(?:es)?|in\.?))?|(?:(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+)[ \t]*(?:["]|inch(?:es)?|in\.?)))/g
Regex101
Basically, the regex is constructed as such:
(?:-[ \t]*)?: Optional negative sign
(?:\d+(?:\.\d*)?|(?:\d+[ \t]+)?\d+[ \t]*\/[ \t]*\d+): Matches whole number (e.g. 10), real number (e.g. 3.45), or fractional number (e.g. 3 1/4, 10/4). Let us denote this part as <number> so that we can see the bigger picture
<number>[ \t]*(?:[']|feet|ft\.?): Feet part. Number and unit optionally separated by space
<number>[ \t]*(?:["]|inch(?:es)?|in\.?): Inch part. Feet part. Number and unit optionally separated by space
(<feet part>(?:[ \t]*(?:-[ \t]*)?<inch part>)?|(?:<inch part>)): Matches string with feet part and optional inch part (optionally separated by hyphen), or only inch part
The code assumes everything on a single line - if you want to match across lines, replace [ \t] with \s.
The regex will pick up valid substrings in non-matching cases - it only cares what it matches is valid, it doesn't care about the context of the match.

Related

Regex to check pattern with numbers

I want to create a regex that does the following
<\numberMAX8>[space or not]<\symbol(-)>[space or not]<\numberMAX8> and max 10 times of all of this - I don't care about end spaces, also numbers must be between 5-8.
To explain it a bit more I'll give a few examples
ex:
5-6 7-6 8-8 6-7 ok
4-7 not ok //because of 4
7 - 6 ok
7-6-6-6 not ok because of the - in the middle
Below is what I have so far without having included the mid spaces.
^([5-8](?:-|\s)[5-8][\s]){1,10}
-> <-//didnt work.
Here you go:
^([5-8]\s*-\s*[5-8]\s*){1,10}$
So the explanation is:
The regex matches a starting number from 5-8 ^[5-8], then an arbitrary number of spaces \s*, then dash -, then arbitrary number of spaces \s*, then a number from 5 to 8 [5-8], then an arbitrary number of spaces \s*, and that pattern from 1 to 10 times {1,10}, and nothing after the pattern $.

How to fetch phone numbers from string using regular expression(Regex)?

I want regex which finds out continues max 12 digits long number by ignoring space, plus (+), parenthesis & dash, e.g:
Primary contact number +91 98333332343 call me on this
My number is +91-983 333 32343
2nd number +1 (983) 333 32343, call me
Another one 983-333-32343
One more +91(983)-333-32343 that's all
121 street pin code 421 728 & number is 9833636363
Currently, I have a regex, which does the job of fetching contact numbers from string:
/* This only work for the first case not for any other
and for last one it outputs "121" */
\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+
So what can be done here to support all the above cases, in short ignoring special characters and length should range from 10-12.
I see that there are numbers ranging from 10 to 13 digits.
You may use
/(?:[-+() ]*\d){10,13}/g
See the regex demo.
Details:
(?:[-+() ]*\d){10,13} - match 10 to 13 sequences of:
[-+() ]* - zero or more characters that are either -, +, (, ), or a space
\d - a digit
var re = /(?:[-+() ]*\d){10,13}/gm;
var str = 'Primary contact number +91 98333332343 call me on this\nMy number is +91-983 333 32343\n2nd number +1 (983) 333 32343, call me\nAnother one 983-333-32343\nOne more +91(983)-333-32343 that\'s all\n121 street pin code 421 728 & number is 9833636363';
var res = str.match(re).map(function(s){return s.trim();});
console.log(res);
The accepted answer will match your criteria but I'd like to propose a more restrictive approach. It is quite specific to the number formats you provided :
test specifically if a string IS a number /^(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}$/
test whether a string contains at least one number : /(\+(\d{1,2})[- ]?)?(\(\d{3}\)|\d{3})[- ]?\d{3}[- ]?\d{4,5}/
I made you a small fiddle where you can try out different regexes on any number of... well numbers : https://jsfiddle.net/u51xrcox/5/.
have fun.

Height and weight Validation in javascript

I need a regular expression to allow a person to enter their height in the following format either
5'10 or 5'10" making the quotation marks optional for the inches. There also could be a space between the feet and inches like 5' 10 or 5' 10".
thanks for the help in advance.
You should be able to use /\d+'\s?\d+"?/ that will allow the numbers to be multiple digits long, requires the single quote after the first number, allows an optional space after the single quote and optionally selects the double quote.
The following will ensure that the number of feet is no greater than 9 and that the inches are between 0 and 11.
/^\d' ?(?:\d|1[0-1])"?$/
If you wanted to make sure the person is at least 2 feet tall, for example, you can just replace the initial \d with the appropriate range.
/^[2-9]' ?(?:\d|1[0-1])"?$/
For weight validation, you can do something like the following, which will ensure the user enters 2 or 3 digits followed by "kg", with an optional space in between. This can easily be adapted for "lbs", or the units can simply be omitted, depending on your requirements.
/^\d{2,3} ?kg$/
I think you might benefit from reading up on regular expressions and testing them out for yourself.

Regex for measurement in hands

Horses in the US are typically measured in hands and inches. A hand is basically just 4 inches. So if a horse was say 62 inches. he would be 15.2 hands tall NOT 15.5. This is because the portion to the right of the decimal is really the remaining inches less than 4, a fraction of a whole hand expressed as a decimal.
Or to be more concise, the portion to the right of the decimal point can only be a single digit that is either a 1, a 2 or a 3. The portion on the left should be a positive integer. The measurement could also just be a whole positive integer if, for example the horses measurement in inches was a multiple of 4.
I am pretty much a dummy on RegEx and I was unable to find an example of this on line and my attempts to modify ones that seem close have ended in failure :)
These are examples of matches: 15.1, 9.2, 100.3, 16
These are examples of non-matches: 15.10, 15.01, 19.4, -15.1, 16.0
Oh and one last thing. I will b using this for validation using JavaScript and perhaps C# as well.
The regex is ^[0-9]+(\.[1-3])?$
in js:
var rx = /^[0-9]+(\.[1-3])?$/;
in c#
var rx = new Regex(#"^[0-9]+(\.[1-3])?$");
If you're only able to have an integer or an integer followed by .1, .2 or .3, you could do that with something like:
\d+(\.[123])?
with appropriate \b (or ^/$) boundary markers at the ends, depending on the method you're using to match (whole string or partial).
If you don't want true internationalised digits, you could replace \d+ with something like [1-9][0-9]* instead.

Reg Expression Javascript for Millions with limit

I am looking to create a regular expression in javascript that does the following:
Allows for 1 or more numbers
Then has an optional period (".")
Then has an optional number of digits up to 6
The context is that i need people to enter in numeric values in the millions and i want them to at least include a 0 if they are entering thousands... so they could enter the following:
1 (would be one million)
0.725 (would be 725k)
10.5 (would be 10M 500K)
I also need to ensure that the value doesn't reach over 725.00 (or 725 million).
Thanks in advance.
That sounds like:
/^(?!\d{4})(?![89]\d\d)(?!7[3-9]\d)(?!72[6-9])(?!725\.0*[1-9])(0|[1-9]\d*)(\.\d{1,6})$/
which means:
doesn't start with four digits (i.e., is less than 1000)
doesn't start with 8 or 9 followed by two digits (i.e., is less than 800)
doesn't start with 73-79 followed by a digit (i.e., is less than 730)
doesn't start with 726-729 (i.e., is less than 726)
doesn't start with 725. followed by zero or more zeroes followed by a nonzero digit (i.e., is less than or equal to 725.00).
starts either with 0, or with 1-9 followed by zero or more digits
after that, optionally a decimal point followed by between one and six digits
That said, I'd actually recommend implementing the above as several separate checks, rather than cramming it all into one regex like the above. In particular, the "is less than or equal to 725.00" check is probably better implemented using numeric comparison; and even if you do want to use a regex for that, you probably want to detect it as a separate error from 0.1asefawe so you can give a more precise error-message.
So basically you want a number that would be multiplied by 10^6 to get the true value.
This sounds like a two-stepper; First, verify that the input string is in a format you expect (you can use a regex for this very easily). Then, parse the string into a number variable and test the actual value. The regex pattern for that would look like "[0-9]{1,3}(\.[0-9]{1,6})?", basically matching a number with up to 3 whole digits and 6 fractional digits, the decimal place and fractional digits being optional. If it matches this pattern, then it's parsable into a number, and you can then perform a quick check that your number <= 725.
I honestly don't think it's feasible to create a single Regex that can validate a proper numeric format AND an inclusive maximum range, but here's a start:
"^(725(\.0{1,6})|(([7][2][0-4]|[7][0-1][0-9]|[1-6][0-9]{2}|[1-9][0-9]|[0-9])(\.[0-9]{1,6})?)$"
This will allow any natural whole number from zero to 724, with any fractional part up to six digits from ".000001" to ".999999". It does this in stages; it will match 720-724, or 700-719, or any three-digit number up to 699, or any two-digit number, or any one-digit number. Then, it will also match the quantity "725" explicitly, with an optional decimal point and up to 6 zeroes.
EDIT: While your comment states that you used this pattern, and it does produce the correct result, I had intended it as a "what not to do"; this pattern will be far more costly to evaluate than the first solution, just to avoid a server-side rule check. And you will have to perform a server-side validation anyway; anything done within the confines of the user's browser should be suspect because the user can disable JavaScript or can even use browser plug-ins like FireBug to make your HTML page behave the way he wants, instead of the way you designed it.

Categories