I trying to make regexp for telephone numbers. Resp. for this input:
+420123456 -> valid
123456 -> valid
Respective I want regexp where can be one or zero + folowed 1 or n digits
function isTelephoneNumber($telephone) {
preg_match("~^[+]{0,1}[0-9]+$~", $telephone,$match);
return (count($match)>0) ? true:false;
}
also tried
"~^\+{0,1}[0-9]+$~"
"~^[+]?[0-9]+$~"
"~^\+?[0-9]+$~"
But something is wrong with the + character.
Your + is not escaped. This one should do the job :
preg_match("/^\+?\d+$/", $telephone,$match);
\+? => zero or one +
\d+ => one or more digits
Related
I want to allow lowercase characters and numbers in username field.
But with following conditions...
Only numbers as username NOT allowed (e.g. only mobile number)
Only lowercase characters allowed (e.g. without any number in username)
Lowercase characters + numbers allowed (e.g. combination of lowercase and numbers)
Minimum length 8 characters required
Maximum length 20 characters allowed
What php regex will do it ?
I tried with following, but it forces lowercase + numbers. Only lowercase username not allowing.
$username_pattern = '/^(?=.*[a-z])(?=.*[a-z])(?=.*\d)[a-z0-9]{8,20}$/';
I want only lowercase and/or lowercase+numbers ( min 8 and max 20 ) in username
Help appreciated.
You can simplify it to not allowing only digits
^(?!\d*$)[a-z0-9]{8,20}$
Explanation
^ Start of string
(?!\d*$) Negative lookahead, assert not only digits till end of string
[a-z0-9]{8,20} Match 8-20 times a char a-z or a digit 0-9
$ End of string
Regex demo | Php demo
$username_pattern = '/^(?!\d*$)[a-z0-9]{8,20}$/';
$userNames = [
"1a3b5678",
"1a3b5678abcd",
"12345678",
"1a3b5678abcddddddddddddddddddddddddddddddd",
"1a3B5678",
"a1"
];
foreach ($userNames as $userName) {
if (preg_match($username_pattern, $userName)) {
echo "Match - $userName" . PHP_EOL;
} else {
echo "No match - $userName" . PHP_EOL;
}
}
Output
Match - 1a3b5678
Match - 1a3b5678abcd
No match - 12345678
No match - 1a3b5678abcddddddddddddddddddddddddddddddd
No match - 1a3B5678
No match - a1
I need to validate a number with those criterias :
Can be float or integer
Scale 4
Precision 2
I tried like this :
pattern="/^[-+]?[0-9]\d*(\.\d+)?$/i",
Some examples :
valid : 2; -1; 0.4; 0.12; 1928; 1827.78; -182.4
invalid : 10000; 0.345; 89374.5;
Thx in advance.
You may use
'/^[-+]?(?:[1-9]\d{0,3}|0)(?:\.\d{1,2})?$/'
See the regex demo.
Details
^ - start of string
[-+]? - either - or +
(?:[1-9]\d{0,3}|0) - a non-capturing group matching either
[1-9]\d{0,3} - a digit from 1 to 9 (non-zero) and any 0 to 3 digits
| - or
0 - a zero
(?:\.\d{1,2})? - an optional non-capturing group matching 1 or 0 occurrences of
\. - a dot
\d{1,2} - 1 or 2 digits
$ - end of string.
Let's say I only allow this kind of format 2015-2016 which contains number and only one dash. How can I do this with preg_match? I tried the following, but with no luck.
$a = '2015-2016';
if(!preg_match('/[^0-9\-]/i',$a)) {
then return not valid data`
}
Hope this will help
preg_match('/^\d{4}-\d{4}$/', $string);
^ Start of the string
\d{4} match a digit [0-9] Exactly 4 times
- matches the character - literally
\d{4} match a digit [0-9] Exactly 4 times
$ End of the string
$a = '2015-2016';
if(!preg_match('/^[0-9 \-]+$/',$a)) {
then return not valid data }
Try that.
If I put * in my regex, it doesn't work, if I put +, it works.
* should mean 0 or more, + should mean 1 or more.
Case with *
$num = ' 527545855 ';
var_dump( preg_match( '/\d*/', substr( $num, 0, 18 ), $coincidencias ) );
var_dump($coincidencias);exit;
Result:
int(1)
array(1) {
[0]=>
string(0) ""
}
Case with +
$num = ' 527545855 ';
var_dump( preg_match( '/\d+/', substr( $num, 0, 18 ), $coincidencias ) );
var_dump($coincidencias);exit;
Result:
int(1)
array(1) {
[0]=>
string(9) "527545855"
}
I thought both should work, but I don't understand why the * doesn't work.
* means 0 or more occurences thus the first occurence is the void string in your test string
The engine will attempting to match starting from index 0.
Since \d* can match an empty string, the engine will return the empty string at index 0.
In contrast, \d+ must match at least one digit, and since the quantifier is greedy, it will return the nearest sequence of digits, while taking as many digits as possible (in your case, it is the whole sequence of digits in the input string).
You answered this in your question:
* should mean 0 or more, + should mean 1 or more.
The first thing that it matched was 0 or more digits.
It would match the digits or nothing
\d* means match 0 to many digits
For Example in
hello 123
the regex \d*
would match 8 times i.e 1 at start(^) ,6 times for hello and 1 time for 123
The * in a regex takes the character before it and looks for 0 or more instances.
The + in a regex looks for 1 or more instances.
Since you aren't really checking around the number (caring for other items) you will get more items back from the regex with * since it allows for the 0 instance clause to be met, which is valid for all your spaces before the number (a space is matching 0 instances of a number). The + ensures at least one is found.
Count character '_' in start line
example :
subject = '_abcd_abc'; // return 1
or
subject = '__abcd_abc'; // return 2
or
subject = '___abcd_abc'; // return 3
everyone help me ~
I use PHP
If you are sure the start of the string contains _, you can do this with just strspn():
echo strspn('___abcd_abc', '_');
// -> 3
If there might be no leading underscores, you can still do this without a regex using strlen and ltrim:
strlen($str) - strlen(ltrim($str, "_"));
This counts the string length, then subtracts the string length without the underscores on the left, the result being the number of underscores.
strspn()
ltrim()
strlen()
Try this:
return preg_match('/^_+/', $str, $match) ? strlen($match[0]) : 0;
If preg_match finds a match, $match[0] will contain that match and strlen($match[0]) returns the length of the match; otherwise the expression will return 0.