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.
Related
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
I'm using Ruby 2.4. I have the following expression for only keeping elements in an array that match a certain pattern:
lines.grep(/^[[:space:]]*\d+/)
How do I write a Ruby expression that keeps elements in an array that both match a pattern and don't match a second pattern? That is, I want to keep elements that match the above, but then also exclude elements that match:
/^[[:space:]]*\d+[:.]/
If my array contains only the element " 123 23:25 ", the result should contain this original element because it starts with a number that doesn't contain a ":" or "." after it.
Use the following regex:
/^[[:space:]]*\d++(?![:.])/
See this regex demo.
Here, ^[[:space:]]* part is the same as in the first regex, \d++ will possessively match 1+ digits (thus, deactivating backtracking) and (?![:.]) will fail any match if those 1+ digits are followed with either : or ..
Details:
^ - start of a line
[[:space:]]* - (may be replaced with \s*) - 0+ whitespace characters
\d++ - 1+ digits matches possessively so that backtracking into the pattern was not possible
(?![:.]) - a negative lookahead that fails the match if : or . is found immediately to the right of the current location (after 1+ digits).
To answer the general case of your question.
Try this
lines.select { |each| each =~ first_pattern && each !~ second_pattern }
Or if you use Ruby 2.3+
lines.grep(first_pattern).grep_v(second_pattern)
The name of grep_v is inspired by the grep -v command line options.
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])$
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()
I want to enter in up to 10, 8 character length numbers in one text field all separated by commas. For example, 12345678,12345678,12345678,12345678.....
The only thing is the numbers can only be 8 characters at max and only up to 10 numbers entered at max. I have it to where I can keep entering numbers but I cannot get it to be with those limitations and I need help.
This is what I have right now: ng-pattern="/^([0-9\s])+(,[0-9\s]+)*$/"
Try this pattern:
^\d{1,8}(,\d{1,8}){0,9}
And works as follows, first, require 1-8 digits, optionally, require comma, 1-8 digits 0 to 9 times.