My regex for testing phone issue - php

I want to validate these phone number formats:
517123123
+48517123123
+48 517 123 123
(48)517123123
(48)517 123 123
517-123-123
48 517-123-123
48/517-123-123
48 517 123 123
I wrote this regex:
(\+?)+(((\(([0-9]+){2,2}\)))|(([0-9]+){2,2})?)+(\/?)+(\s?)+(([0-9]+){9,9}|([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3}(\s|-){1,1}([0-9]+){3,3})
The problem is that it's makes big numbers like 8978978979878978967 valid. Where is my mistake?

Looking at just the end of the regex, I see something that you seem to be doing in multiple places;
([0-9]+){3,3}
The + says at least one repeat of [0-9], which makes 1111111111111 a perfectly valid match. You then limit it to exactly 3 of those matches, which can still be a very long number.
If you want exactly 3 digits, remove the +.

may be you lost anchors.... however, use my regex ^(\+?(\(\d{2}\)|(\d{2})|(\d{2}[/ ])))?((\d{3} \d{3} \d{3})|(\d{3}-\d{3}-\d{3})|(\d{9}))$

At the moment I can't see what your regex is doing, there is too much superfluous stuff in it.
You have too many groups
You want to repeat optional characters!?
e.g.:
(\+?)+, you don't need a group around and you don't want to repeat that, so \+? is what you want here.
(\s?)+, do you want to say "0 or more whitespaces"? Then \s* is what you need.
When you write e.g. {9,9}, then you can remove one digit, {9} is the same.
You are nesting quantifiers, thats the place where you allow too many characters. You have multiple places, where you do ([0-9]+){9,9}, that means 1 or more digits and repeat that 9 times.

Related

PHP - Find number between 2 Unicode characters

Simple problem but i sux at regular expressions so i need here ur help.
What do i need to type to find a number between two first signs: •
Find out its codes but it doenst help me much: http://www.fileformat.info/info/unicode/char/2022/index.htm
Do you know what should i type in for example preg_match function to make it work?
Example:
• 12345 • TESTTESTTEST
Example Output:
12345
Thanks in advance!
To match a specific Unicode code point, use \x{FFFF} where FFFF is the hexadecimal number of the code point you want to match. You can omit leading zeros in the hexadecimal number between the curly braces. Since \x by itself is not a valid regex token, \x{1234} can never be confused to match \x 1234 times. It always matches the Unicode code point U+1234. \x{1234}{5678} will try to match code point U+1234 exactly 5678 times.
Anyway, what you're probably looking for is something like this:
\x{2022} (\d*) \x{2022}
As for the (\d*) part, it basically means match any digit infinite times, and assign this bit of the pattern as a match (braces stand for capture groups)
Actually i found out a way to do it a bit easier.
I used preg_match() with $pattern = "/[0-9]{1,}/";
Huh xD

Regex for exact number / sting match in between

I want to make a regex where I can find the exact number in between a string.
eg. finding the number 2 in 3, 5, 25, 22,2, 15
What I have is /*,2,*/.
But with this regex it matches 22,25 or just anything with a 2 in it. I want it where only match where the number 2 itself is between the commas or without the commas standing alone.
*Update
Both the number(needle) i look for and string(haystack) where i seek it can vary.
Eg if the number i seek is always 2
I want to find them in 2,3,44,23,22,1 or 3,4,22,5,2 or 2 and i should be able to find one match for each of the group of numbers.
You should probably use boundaries (\b) so a leading/trailing comma isn't required.
/\b2\b/
You should do this instead:
,(\d), #for any single digit
,(2), #for 2 in particular
Demo: http://regex101.com/r/vP6jI1

PHP Regex - accept all positive numbers except for 1

Regexes and I have a relationship of love and hate. I need to match (accept) all numbers except for number 1 and 0. Seeing it as math and not string, number >= 2 should be matched. Also please consider this is part of a Zend route param (reqs) so I have to go with regex unless I want to extend Route class, and etc. etc. :)
103 => 103
013 => 013
201 => 201
340 => 340
111 => 111
001 => no match
010 => 010
100 => 100
1 => no match
000 => no match
00 => no match
0 => no match
I've tried some variations of [^1][|\d+] (trying to nail one digit at a time :D) but so far I've failed horribly :(
Nailed it!!
The regex I was looking for appears to be the following
^([2-9]|[2-9]\d|[1-9]\d{1,})$
Just use negative lookahead to exclude patterns with all zeroes that optionally end in an one:
/^(?!0*1?$)\d+$/
If you read it without the parens, this regex matches anything that consists of one or more decimal digits. The parens contain an assertion that causes the regex to match only if the pattern 0*1?$ cannot be matched beginning at the start of the input, so this removes the scenario of all zeroes and one with any number of prepended zeroes.
Use negation on the result for matching all zeroes and ones
if(!preg_match("^[01]+$",$string)) {...}
Your thinking of it the wrong way.
^[01]+$
will match all that onyl contain 0 or 1.
If that matches reject it, if it doesnt match check its a valid number and you should have a match
//if the numbers only include 0 or 1 reurn false;else will return the numbers
function match_number($number){
if(preg_match("/^[01]*$/", $number) > 0){
return false;
} else {
return $number;
}
}
The following regex will match any number (not digits, but number as a whole) >= 2
^([2-9]|[2-9]\d|[1-9]\d{1,})$
Thanks for all the valuable help in the answers, some were really helpful and helped me reach the desired result.

How to match those numbers?

I have an array of numbers, for example:
10001234
10002345
Now I have a number, which should be matched against all of those numbers inside the array. The number could either be 10001234 (which would be easy to match), but it could also be 100001234 (4 zeros instead of 3) or 101234 (one zero instead of 3) for example. Any combination could be possible. The only fixed part is the 1234 at the end.
I cant get the last 4 chars, because it can also be 3 or 5 or 6 ..., like 1000123456.
Whats a good way to match that? Maybe its easy and I dont see the wood for the trees :D.
Thanks!
if always the first number is one you can use this
$Num=1000436346;
echo(int)ltrim($Num."","1");
output:
436346
$number % 10000
Will return the remainder of dividing a number by 10000. Meaning, the last four digits.
The question doesn't make the criteria for the match very clear. However, I'll give it a go.
First, my assumptions:
The number always starts with a 1 followed by an unknown number of 0s.
After that, we have a sequence of digits which could be anything (but presumably not starting with zero?), which you want to extract from the string?
Given the above, we can formulate an expression fairly easily:
$input='10002345';
if(preg_match('/10+(\d+)/',$input,$matches)) {
$output = $matches[1];
}
$output now contains the second part of the number -- ie 2345.
If you need to match more than just a leading 1, you can replace that in the expression with \d to match any digit. And add a plus sign after it to allow more than one digit here (although we're still relying on there being at least one zero between the first part of the number and the second).
$input='10002345';
if(preg_match('/\d+0+(\d+)/',$input,$matches)) {
$output = $matches[1];
}

PHP regular expressions (phonenumber)

I'm having some trouble with a regular expression for phone numbers. I am trying to create a regex that is as broad as possible for european phone numbers. The phone number can start with a + or with two leading 0's, followed by a number in between 0 and 40. this is not necessary however, so this first part can also ignored. After that, it should all be numbers, grouped into pairs of at least two, with a whitespace or a - inbetween the groups.
The regex I have put together can be found below.
/((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}/
This should match the following structures
0031 34-56-78
0032123456789
0033 123 456 789
0034-123-456-789
+35 34-56-78
+36123456789
+37 123 456 789
+38-123-456-789
...
What it also matches according to my javascript
+32 a54b 67-0:
So I must have made a mistake somewhere, but I really can't see it. Any help would be appreciated.
The problem is that you don't use anchors ^ $ to define the start and ending of the string and will therefore find a match anywhere in the string.
/^((\+|00)+[0-4]+[0-9]+)?([ -]?[0-9]{2,15}){1,5}$/
Adding anchors will do the trick. More about these meta characters can be found here.
Try this, may be can help you.
if (ereg("^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$",$var))
{
$valid = true;
}
Put ^ in the beginning of the RegExp and $ in the end.

Categories