How to do national insurance number regular expression in angularjs - angularjs

This is my Regular Expression ^[A-Z]{2}[0-9]{6}[A-Z]{1}?$
I am using this expression in angularjs ng-pattern-restrict . I can't type anything in the input field.
If i am use ^[A-Z]{0,2}[0-9]{0,6}[A-Z]{0,1}?$ : this expression then i can type three letters but can't type number.
If i will type two letters then only i can type six numbers.If i type three letters then i can't type numbers
I need The result like this: PP123456P Expression.
Can Anyone Help me

^[A-Z]{0,2}[0-9]{1,6}[A-Z]{1}?$
Should work, the reason you could type 3 letters is that the second group was allowed to be size 0-6 and then when you put in 3 letters it has matched the first 2 letter group, the numeric group with a 0 length match and finally the last Alpha. by changing it to 1-6 you force the entry to be between 1 and 6 numbers.

You seem to have a bit of redundancy in your regular expression and not quite sure what you were trying to do with [A-Z]{1}?, is this supposed to be an optional letter?
Although simplistic compared with the actual validation rules of National Insurance numbers you could use
^[A-Z]{2}[0-9]{6}[A-Z]$
Breaking that down...
^ Must match at the start of the string
[A-Z]{2} Exactly 2 letters in the range A-Z
[0-9][6] Exactly 6 digits in the range 0-9
[A-Z] Exactly 1 letter in the range A-Z
$ There must be nothing else following the previous matches

Related

Counting number of variables in an array not passing test cases

So I am doing a program on Hackerrank.
The problem is
You have been asked to help study the population of birds migrating across the continent. Each type of bird you are interested in will be identified by an integer value. Each time a particular kind of bird is spotted, its id number will be added to your array of sightings. You would like to be able to find out which type of bird is most common given a list of sightings. Your task is to print the type number of that bird and if two or more types of birds are equally common, choose the type with the smallest ID number.
For example, assume your bird sightings are of types . There are two each of types and , and one sighting of type . Pick the lower of the two types seen twice: type .
Function Description
Complete the migratoryBirds function in the editor below. It should return the lowest type number of the most frequently sighted bird.
migratoryBirds has the following parameter(s):
arr: an array of integers representing types of birds sighted
Input Format
The first line contains an integer denoting , the number of birds sighted and reported in the array .
The second line describes as space-separated integers representing the type numbers of each bird sighted.
Constraints
It is guaranteed that each type is , , , , or .
Output Format
Print the type number of the most common bird; if two or more types of birds are equally common, choose the type with the smallest ID number.
This is my code. It is passing all but one of the test cases. Can you help?
x = []
for i in range (0,len(arr)):
x.append(arr.count(arr[i]))
for i in range(0,len(x)):
if x[i] == max(x):
out = i
return out
I don't see that your code passes any test cases, even with the indentation fixed. Your input size can be as large as 105, so that pretty much eliminates any O(n2) solutions that require traversing the entire list, which is unnecessary. The culprit here is arr.count(), which traverses the entire list for every element. You probably meant to traverse the list for every bird type, which results in 5 traversals of the input list:
x = []
for i in range(1, 6):
x.append(arr.count(i))
out = 0
best = x[0]
for i in range(len(x)):
if x[i] > best:
best = x[i]
out = i
return out + 1
The important restriction to note in this problem is that there are only 5 bird types (numbered 1-5), so we can initialize a counts array of length 6 with a dummy element at position 0 so indices map to counts neatly. Then, we can iterate through our input bird type list in one pass, updating our counter array. Lastly, take the max index of the counter array. The "smallest id for ties" restriction is automatically handled by the way max() works--it'll take the smallest index automatically. Here's the code:
counts = [-1, 0, 0, 0, 0, 0]
for bird in arr:
counts[bird] += 1
return counts.index(max(counts))
Note that the variable names are descriptive. This helps ensure that the purpose of each variable is clear and reduces bugs and erroneous assumptions about the program state.

ng-pattern for exclusion group of numbers

I have a list of exclusion numbers.
for example
(400276|400615|402914|404625)
the pattern should not let me enter into the input any of these numbers as the first 6 digits
example
400276123 .BAD. because the value init with a number to exclude
400277123 .OK
I try something like that
"^[^] (400|405)"
but is not working
how can I use a pattern for exclude this first 6 digits
Your pattern - ^[^] (400|405) - matches the start of the string, then any char, a space, and either 400 or 405.
What you need is a negative lookahead:
/^(?!400276|400615|402914|404625)/
^^^ ^
It will fail the match of a string that starts with these values.
See the regex demo.

How would you set the NgPattern of an Angular NgMessages Application for two given years as a date range [duplicate]

I'm trying to use the range pattern [01-12] in regex to match two digit mm, but this doesn't work as expected.
You seem to have misunderstood how character classes definition works in regex.
To match any of the strings 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, or 12, something like this works:
0[1-9]|1[0-2]
References
regular-expressions.info/Character Classes
Numeric Ranges (have many examples on matching strings interpreted as numeric ranges)
Explanation
A character class, by itself, attempts to match one and exactly one character from the input string. [01-12] actually defines [012], a character class that matches one character from the input against any of the 3 characters 0, 1, or 2.
The - range definition goes from 1 to 1, which includes just 1. On the other hand, something like [1-9] includes 1, 2, 3, 4, 5, 6, 7, 8, 9.
Beginners often make the mistakes of defining things like [this|that]. This doesn't "work". This character definition defines [this|a], i.e. it matches one character from the input against any of 6 characters in t, h, i, s, | or a. More than likely (this|that) is what is intended.
References
regular-expressions.info/Brackets for Grouping and Alternation with the vertical bar
How ranges are defined
So it's obvious now that a pattern like between [24-48] hours doesn't "work". The character class in this case is equivalent to [248].
That is, - in a character class definition doesn't define numeric range in the pattern. Regex engines doesn't really "understand" numbers in the pattern, with the exception of finite repetition syntax (e.g. a{3,5} matches between 3 and 5 a).
Range definition instead uses ASCII/Unicode encoding of the characters to define ranges. The character 0 is encoded in ASCII as decimal 48; 9 is 57. Thus, the character definition [0-9] includes all character whose values are between decimal 48 and 57 in the encoding. Rather sensibly, by design these are the characters 0, 1, ..., 9.
See also
Wikipedia/ASCII
Another example: A to Z
Let's take a look at another common character class definition [a-zA-Z]
In ASCII:
A = 65, Z = 90
a = 97, z = 122
This means that:
[a-zA-Z] and [A-Za-z] are equivalent
In most flavors, [a-Z] is likely to be an illegal character range
because a (97) is "greater than" than Z (90)
[A-z] is legal, but also includes these six characters:
[ (91), \ (92), ] (93), ^ (94), _ (95), ` (96)
Related questions
is the regex [a-Z] valid and if yes then is it the same as [a-zA-Z]
A character class in regular expressions, denoted by the [...] syntax, specifies the rules to match a single character in the input. As such, everything you write between the brackets specify how to match a single character.
Your pattern, [01-12] is thus broken down as follows:
0 - match the single digit 0
or, 1-1, match a single digit in the range of 1 through 1
or, 2, match a single digit 2
So basically all you're matching is 0, 1 or 2.
In order to do the matching you want, matching two digits, ranging from 01-12 as numbers, you need to think about how they will look as text.
You have:
01-09 (ie. first digit is 0, second digit is 1-9)
10-12 (ie. first digit is 1, second digit is 0-2)
You will then have to write a regular expression for that, which can look like this:
+-- a 0 followed by 1-9
|
| +-- a 1 followed by 0-2
| |
<-+--> <-+-->
0[1-9]|1[0-2]
^
|
+-- vertical bar, this roughly means "OR" in this context
Note that trying to combine them in order to get a shorter expression will fail, by giving false positive matches for invalid input.
For instance, the pattern [0-1][0-9] would basically match the numbers 00-19, which is a bit more than what you want.
I tried finding a definite source for more information about character classes, but for now all I can give you is this Google Query for Regex Character Classes. Hopefully you'll be able to find some more information there to help you.
This also works:
^([1-9]|[0-1][0-2])$
[1-9] matches single digits between 1 and 9
[0-1][0-2] matches double digits between 10 and 12
There are some good examples here
The []s in a regex denote a character class. If no ranges are specified, it implicitly ors every character within it together. Thus, [abcde] is the same as (a|b|c|d|e), except that it doesn't capture anything; it will match any one of a, b, c, d, or e. All a range indicates is a set of characters; [ac-eg] says "match any one of: a; any character between c and e; or g". Thus, your match says "match any one of: 0; any character between 1 and 1 (i.e., just 1); or 2.
Your goal is evidently to specify a number range: any number between 01 and 12 written with two digits. In this specific case, you can match it with 0[1-9]|1[0-2]: either a 0 followed by any digit between 1 and 9, or a 1 followed by any digit between 0 and 2. In general, you can transform any number range into a valid regex in a similar manner. There may be a better option than regular expressions, however, or an existing function or module which can construct the regex for you. It depends on your language.
Use this:
0?[1-9]|1[012]
07: valid
7: valid
0: not match
00 : not match
13 : not match
21 : not match
To test a pattern as 07/2018 use this:
/^(0?[1-9]|1[012])\/([2-9][0-9]{3})$/
(Date range between 01/2000 to 12/9999 )
As polygenelubricants says yours would look for 0|1-1|2 rather than what you wish for, due to the fact that character classes (things in []) match characters rather than strings.
My solution to keep mm-yyyy is ^0*([1-9]|1[0-2])-(20[2-4][0-9])$

Excel: How to check for repeated numbers inside a cell

I have an excel spreadsheet with numbers from 000 to 999 and am trying to find repeated numbers inside a cell.
(So for example, printing 1 if the number is 022 , 555 or 115 and 0 if it isn't)
So far, I have not been able to find a solution.
Feel free to ask for more information and thanks in advance.
This will do: =IF(COUNT(SEARCH(REPT({0,1,2,3,4,5,6,7,8,9},2),A1))>0,1,0)
Note: If value in cell A1 contains 2 repeated digits it will show 1 else 0. You can customize the repetition limit by changing 2 in the part 8,9},2).
You could try this one if you wanted to find repeated digits not necessarily next to each other:-
=IF(MAX(LEN(A1)-LEN(SUBSTITUTE(A1,{0,1,2,3,4,5,6,7,8,9},"")))>1,1,0)
If the numbers are stored as 3-digit numbers and you wanted it to work for (e.g.) 001, would need:-
=IF(MAX(LEN(TEXT($A1,"000"))-LEN(SUBSTITUTE(TEXT($A1,"000"),{0,1,2,3,4,5,6,7,8,9},"")))>1,1,0)
If your data is in Range "A1:A100" and you want to locate repeated numbers in the range for instance, enter =IF(COUNTIF(A:A,A1)>1,1,0) in cell B1 and fill down. But if you want to check repetitions of specific numbers like 022, 555 or 115, enter =IF(OR(AND(A1=022,COUNTIF(A:A,A1)>1),AND(A1=555,COUNTIF(A:A,A1)>1),AND(A1=115,COUNTIF(A:A,A1)>1)),1,0) in cell B1 and fill down.
being a number, use arithmetics to break it into digits and then check if all are different.
the formula is
=INT(NOT(AND(INT(A1/100)<>INT(MOD(A1,100)/10),INT(A1/100)<>MOD(A1,10),INT(MOD(A1,100)/10)<>MOD(A1,10))))
let's analyze it step by step
first, INT(A1/100) extracts the first digit (the integer division by 100); then INT(MOD(A1,100)/10) extracts the second digit (the integer division by 10 of the modulo 100); and MOD(A1,10) extracts the last digit (the modulo 10).
next there are the three comparisons of difference <> first with second, second with third and first with third, combined with AND() and finally take the result, negate it NOT() and transforming it into an integer 0 or 1 with INT()

Bit Operations with Enumerated Type

I'm having some trouble getting started with a lab of mine. The lab has the following instructions:
Given a value int input = 0b10010110 determine what sports an individual likes. Assume there will be no errors in this input. From left to right, each position represents the following: Football, Baseball, Volleyball, Swimming, Softball, Soccer, Hockey, Lacrosse.
If there is a 1 is in that position, then the person likes that sport. Based on the “binary” input given, output to the screen all of the sports that the user enjoys. For the given binary number, the user likes, football, swimming, soccer and hockey.
Do not make an array of characters.
Be sure to use an enumerated data type.
I'm not sure how I can compare each position of the string to tell whether it is a 1 or 0. One idea I have is to use an enumerated type where I set each sport to a ten digit number where only its appropriate position is 1
enum sport{
Football = 0010000000,
Baseball = 0001000000,
Volleyball = 0000100000,
... ,
Lacrosse = 0000000001
};
I would then shift left/ right on the given value "input" the appropriate amount of times to leave only the specified position with its original value and to set all other values to 0. For Football:
input << 2; input >> 9; input << 7;
So the new set value would be 0010000000. Then I'd be able to compare the number as a whole. I would have to that for each case, but I cannot think of a different way to do it.
Am I completely off? Is there a more functional way to check the value of each position using bit operations? Thanks for any assistance in advance.
Use bitwise AND operator &:
if (input & Football) {
// Football bit is set
} else {
// Football bit is not set
}
Also note that the 0b prefix is a compiler extension but is not standard C. And in your enum values, a number starting with the 0 prefix is in octal format (you have to fix this). I suggest you to use hexadecimal format for bit manipulation.
You could make things a bit more readable if you define you sport values by shifting a 1 to the left by the appropriate number of places; in fact, you could use a simple enumeration for the sport values, and use that to specify how many places to shift the bit.

Resources