Regular expressions: meaning of {2, 4} - objective-c

What is the meaning of {2,4} when validating an email with the following regular expression:
NSString *emailRegEx = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];

It mean words length is minimum 2 and maximum 4 and it takes Capital A to Z and small a to z characters..

{2,4} means the string has minimum 2 characters and maximum 4 characters (The length of string should be greater than or equal to 2 and less than or equal to 4).
For example: In email ids and after dot, .com,.in, .uk so on...

In above regular expression, {2,4} means you can take occurrence of [A-Za-z] between 2 to 4 inclusive.
Ref
http://www.regular-expressions.info/reference.html
http://www.regextester.com/

Related

how to use positioning/range in regexp

I have a product code where the references always follows this pattern: XX00XX000XX. Characters 1 and 2 are always a combination of 2 letters, 3 to 4 a combination of 2 numbers, 5 to 6 letters, 7 to 10 numbers and 10 to 11 letters again (they`re always varying so it'll never be the same).
I want to do a regexp_contains (or another variant) that matches by position like; position 1 - 2 must be [[:alpha:]], 3 - 4 [[:digit:]], and so on.
(I need this to find product codes that match the reference pattern inside sell links, but I can't find any clear explanation on how to use positioning on regex statements...)
You can use character classes for this.
[a-zA-Z][a-zA-Z]\d\d[a-zA-Z][a-zA-Z]\d\d\d[a-zA-Z][a-zA-Z]
This regex contains the class [a-zA-Z] and \d, which matches letter and digit respectively. This explicitly checks, first character is a letter, second character is a letter, third character is a digit, etc.
The character classes match 1 character in the set specified, so [a-zA-Z] matches any letter, [13579] will match any odd number, etc.

regex - match exactly 10 digits with atleast one symbol or spaces between them

I'm trying to write a query in oracle sql to get rows which has invalid 10 digit numbers, ie with other symbols in between them.
For example:
(111) 111-1111 #10 digit number with some symbols and spaces in between
111-111-1111
(111)111-1111
111)111-1111
(111) 11 1-1111
ie, It should match exactly 10 digit numbers which are non consecutive because it has some symbols in it.
So it should not match the following example:
111 #consecutive 3 digit number
11 1 #3 digit number with spaces
11-1 #3 digit number with symbol in between
1111111111 #consective 10 digit number
And I'm using REGEXP_LIKE, something like this
select * from table where REGEXP_LIKE(column, ?)
Any help is much appreciated. Thanks.
You could use a combination of a regex and length; the latter to exclude a pure 10-digit number without other characters:
regexp_like(col, '^[ .()-]*(\d[ .()-]*){10}$') and length(col) > 10
In the [.()-] class you would list all the characters that you would allow as symbols among the digits. Note that - needs to be the last in that list or else be escaped.
If you would allow any non-digit to occur among the 10 digits, you can use \D:
regexp_like(col, '^\D*(\d\D*){10}$') and length(col) > 10
So: the string should have length greater than 10, and the total number of digits must be exactly 10. This can be done without regular expressions (which should make it faster):
... where length(str) > 10 and
length(str) = 10 + length(translate(str, 'z0123456789', 'z'))
translate will translate the letter z to itself and all the other characters (digits) to nothing. Having to include the z is annoying, but unavoidable; translate will return NULL if any of its arguments is NULL. The second condition says the length of the input str is exactly 10 more than the length of the string with all digits removed - so there are exactly 10 digits.

how we can find regular expression for following strings

Find regular expressions representing the following set:
The set of all strings over {a,b} in which the number of
occurrences of a is divided by 3.
The set of all strings over {0,1} beginning with 00
You can draw out a DFA and use that to find the regular expression.
For example, for 1., it would be
Then you use convert this into a regular expression. This is one way
For 1, you need an expression that gives every possible way of having a string over {a,b} with the occurrences of a divisible by 3. There can be 0 a's since 0 is divisible by 3. There can be 3 a's, 6 a's, 9 a's, and so on. An expression for this is (bababab)+b. The second term allows for the possibility of 0 a's and any amount of b's since 0 a's is divisible by 3. The first term accounts for all other possibilities of strings with a number of a's divisible by 3.
For 2, the set of all strings over {0,1} is (0+1)* and if it must begin with 00, then the regex is simply 00(0+1)*

count occurences of string in substring with condition

I need to count how often a number is present in a string. it should count EVERY occurence with a whitespace in front, except those followed by a =.
For example:
If i need to know how many "1" there are in this string: this is a 1 ramdnom string with 2 numbers 1 with 1=something it should return 2, as the third one is followed by an =
To find the occurrences I am using this: occurences = mystring.Split(" 1").Length - 1
But how to exclude those followed by a =?
Thanks
Something like,
Dim occurrences = Regex.Matches(yourString, "\W[0-9]([^=]|$)").Count
If you'd like to do replacements, use a Regex.Replace overload.
Breaking it down, this expression matches
\W // any whitespace character
[0-9] // any deciaml digit
( // either
[^=] // not =
| // or
$ // the end of the string
)

NSString get 100 characters from start

How to get a substring from NSSTring from index 0 to 99. i.e. first 100 characters
[myString substringToIndex:100]
You need to be sure that index 100 is valid, i.e. length of string is at least 100. Otherwise an exception will be thrown.
[str substringToIndex: 100]