Regular expression for a range of numbers [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
im trying to do a regex but cant understand so well.
What i want is to input the following:
09XYZZZZZZ
Where X is from 6 to 9, Y 1 to 9, and Z 0 to 9. All together, only 12 digits length.
Thanks !

The solution depends heavily on the type of regex used. In Perl regex, for example, you can write:
/09[6-9][1-9][0-9]{6}/
This matches exactly the string 09, then exactly one digit from 6 to 9, then exactly one digit from 1 to 9, and lastly exactly 6 digits from 0 to 9.
In POSIX regex, the {6} notation does not exist. You will have to repeat the range 6 times there.
Please note that your example pattern is only 10 digits long, while your description says 12 digits. If you want a maximum of 12 digits, and an arbitrary number of Z at the end, you can use
/09[6-9][1-9][0-9]+/
in Perl or
/09[6-9][1-9][0-9][0-9]*/
in POSIX - the + notation does not exist there.

09[6-9][1-9][0-9]{8}
Explain regex

/^09[6-9][1-9][3-9]{6}.{2}$/
That should do what you want, assuming the last 2 digits can be anything since you didn't specify them.
Breaking it down:
^ matches the start of the string
09 matches "09" exactly
[6-9] and similar define ranges for a character
[3-9]{6} says we want 6 occurrences between 3 and 9
.{2} means 2 of anything.

Actually, POSIX does have ranges, see re_format(7).
Common are POSIX BASIC REGULAR EXPRESSIONS:
09[6-9][1-9][0-9]\{6\}
Adjust the 6 to 8 if the twelve digits are correct. You can write this for a more flexible range of numbers:
09[6-9][1-9][0-9]\{6,8\}
Sometimes, you have POSIX ERE though:
09[6-9][1-9][0-9]{6}
POSIX ERE support + like PCRE, BRE don’t but you can use \{6,\} for open end there. POSIX regex do not, like PCRE (see Dominik’s answer for them), need delimiters as part of the regular expression.
See perlre(1) and perlretut(1) for Perl regular expressions, to which PCRE is mostly compatible.
PHP uses PCRE these days, in case you wonder.

Related

Why is String.indexOf functioning like this? [duplicate]

This question already has answers here:
Seemingly identical strings fail comparison
(4 answers)
Closed 2 years ago.
I'm trying to match some text based on a query that the user inputs. After encountering some issues, I found out this rather odd behaviour of String.indexOf that I simply cannot understand:
If I try to match a query without diacritics against a string with diacritics, it works: (not sure why)
"brezzel cu brânză".indexOf("bra")
11
But matching the same string with another letter after it, doesn't work:
"brezzel cu brânză".indexOf("bran")
-1
(tested both in Chrome & Firefox, same behaviour)
Is this a documented behaviour that I'm unaware of or what exactly is happening here?
If I remember correctly, js characters are encoded in 2 bytes. But many other unicode chars encoded 4 bytes. Now the char â is 4 bytes. The first 2 bytes is a, thats why the first case works. Use the escape function to see:
escape("brezzel cu brânză")
"brezzel%20cu%20bra%u0302nza%u0306"
see that %20 is space, followed by bra and then you have %u0302 which together with previous a, encodes â.
Probably you can tell the rest. Test it if you want to:
'a' + String.fromCharCode('0x0302') //â

Need regex for mobile number start with 61 to 99 with 10 digit number

Requirement is mobile number should start with 61 to 99
like 61xxxxxxxx, 62xxxxxxxxx... , 99xxxxxxxxxx
Need regular expression to match this case.
If mobile no is start with 0 or 11,12 or anything less than 61 then it should be invalid
Mobile no is max 10 digits, no country code needed.
You're probably better off using whatever programming tool you have to evaluate whether the first 2 digits are in range, far simpler and probably performant too. However, if you strictly want to use regex, this will do-
(?:6[1-9]|[7-9][0-9])\d{8}$
Here's the demo
It essentially, checks the first digit, if it's a 6, the next digit should be in range [1-9], if it's a 7, 8 or 9 (i.e range [7-9]), the next digit can be in range [0-9]. Then there should be 8 digits that follow.
Ofcourse, this above is a simple and easy to understand solution. Essentially checking each first digit and then matching the next. However if your regex flavor supports negative lookbehind, you could probably shorten this a bit more (sacrificing readability for brevity) but I do prefer this.
You could generate the prefix for the numbers and add a pattern for the remaining 8 digits.
Something like this
const regexp = new RegExp('('+[...Array(39).keys()].map(key => key + 61).join('|') + ')\\d{8,8}')

Regular Expression in JavaScript having exactly 6 digits and at least two different digits [duplicate]

This question already has answers here:
Regular expression limit string size
(3 answers)
Closed 2 years ago.
I want to make sure that I have exactly 6 digits and not all of them are the same (222222 is not acceptable but 122222 or 211111 or 122223 are acceptable).
I am using this:
^(?=\d{6})(\d)\1*(?!\1)\d+$
But this only satisfies minimum 6 digits and not exactly 6 digits.
(source: Regex to match number with different digits and minimum length)
You may use this bit simplified and more efficient regex:
^(\d)(?!\1+$)\d{5}$
RegEx Demo
RegEx Details:
^(\d): Match and capture a digit at the start
(?!\1+$): Negative lookahead to assert that same digit is not repeated till end.
\d{5}$: Add 5 more digits before end

Regex match for several similar string formats [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What is the best way to match a set of strings in this format:
AA99999999 2 fixed characters 8 numbers
ABC99999999 3 fixed characters 8 numbers
AD999999999 2 fixed characters 9 numbers
also
BA999999999 2 fixed characters 9 numbers
BB99999x999 2 fixed characters 5 numbers 1 character 3 numbers
BHA-9999#9 4 fixed characters 4 numbers 1 fixed character 1 number
BHGD99999 4 fixed characters 5 numbers
For the first match I wrote a regex like this
[a-zA-Z]{3}\d{8}|([a-zA-Z]{2}\d{9}|[a-zA-Z]{2}\d{8})
This works but I have no idea about the second. Is there are regex that will match these formats?
It's pretty hard to tell exactly what you want here, but I've combined them into two regex expressions rather than using 4, because it's faster. I've used either \w or . where you used [a-zA-Z] because, again, it's difficult to tell what you want. Provide more information and I'll modify the answer.
\w{2}\d{5}.\d{3}|.{4}\d{4}.?\d
They're simply more of the same:
([a-zA-Z]{2}\d{9}|([a-zA-Z]{2}\d{5}[a-zA-Z]\d{3}|[a-zA-Z-]{4}[a-zA-Z#]\d)
assuming where a '-' pops up a [a-zA-Z-] is the group of values that can match there and likewise for [a-zA-Z#] for the '#'
I would take fixed characters to mean that the specification is to have a case where if the first 3 characters are ABC followed by 8 numbers instead of widlcard letters in which case the patterns for each case would be as follows:
AA99999999 2 fixed characters 8 numbers
AA\d{8}
AD999999999 2 fixed characters 9 numbers
AD\d{9}
ABC99999999 3 fixed characters 8 numbers
ABC\d{8}
also
BA999999999 2 fixed characters 9 numbers
BA\d{9}
BB99999x999 2 fixed characters 5 numbers 1 character 3 numbers
BB\d{5}x\d{3}
BHA-9999#9 4 fixed characters 4 numbers 1 fixed character 1 number
BHA-\d{4}#\d
BHGD99999 4 fixed characters 5 numbers
BHGD\d{5}
var patt1 = /^\w\w[\w\d]{1}[\d\w-]{1}\d{2}[\d\w]{1,4}[\d#]{1}\d+$/;
If this is the case you will need to either put each of these in as alternatives or do some logic matching or iterate through each of them to test until a match is found.
Or do you mean capital letters instead of fixed characters?
Maybe you could play with this fiddle.
https://jsfiddle.net/shotgundriver/r812c00v/6/
Is this a homework problem or real world?

Regular expression for numbers with decimal [duplicate]

This question already has answers here:
Regular Expression to accept only positive numbers and decimals
(6 answers)
Closed 9 years ago.
In my phonegap app I need to validate the textbox with regular expression which should contain only numbers and two decimal places.
Here is sample I have tried:
rexp: /^-?\d+\.?\d*$/
This code allows only numbers but after the dot(.) its accepting many numbers. But I should allow only two. Please guide me to solve this issue.
You can make the number of matches explicit using the "{x}" modifier:
rexp: /^-?\d+\.?(\d{2})?$/
I suspect that you want to only have the two digits when there is a dot specified. Then you would modify you regexp as follows:
rexp: /^-?\d+(\.\d{2})?$/
I have created a JSFiddle to show the workings of the regexes and the difference between them: http://jsfiddle.net/q8NAz/

Categories

Resources