Hey could someone help me to test if a string matches 3 double digit figures separated by a colon? For Example:
12:13:14
I understand I should be using preg_match but I can't work out how
Preferably the first number should be between 0 and 23 and the second two numbers should be between 0 and 59 like a time but I can always work that out with if statements.
Thanks
This answer does correct matching across the entire string (other answers will match the regexp within a longer string), without any extra tests required:
if (preg_match('/^((?:[0-1][0-9])|(?:2[0-3])):([0-5][0-9]):([0-5][0-9])$/', $string, $matches))
{
print_r($matches);
}
else
{
echo "Does not match\n";
}
You could use preg_match with number comparissons on $string = '23:24:25';
preg_match('~^(\d{2}):(\d{2}):(\d{2})$~', $string, $matches);
if (count($matches) != 3 || $matches[1] > 23 || $matches[2] > 59 || $matches[3] > 59 ......)
die('The digits are not right');
Or you can even ditch the regular expresions and use explode with numeric comparisons.
$numbers = explode(':', $string);
if (count($numbers) != 3 || $numbers[0] > 23 || $numbers[1] > 59 || $numbers[2] > 59 ......)
die('The digits are not right');
$regex = "/\d\d\:\d\d\:\d\d/";
$subject = "12:13:14";
preg_match($regex, $subject, $matches);
print_r($matches);
if (preg_match ('/\d\d:\d\d:\d\d/', $input)) {
// matches
} else {
// doesnt match
}
\d means any digit, so groups of two of those with : in between.
Related
I would check a string if contains at least:
2 letters (a-z) CASE INSENSITIVE
4 digits (0-9)
the order is not important. It could be a1234b, abcd1234, 4444aa etc etc.
My actual regex is
if (preg_match("/[a-z][^a-z]*[a-z]*[0-9]/i",$string)) {
echo 'secure';
$continue = true;
}
and it doesn't function if string start with a digit. Thank you
^(?=(?:.*[a-zA-Z]){2})(?=(?:.*[0-9]){4})\w+$
You can use lookahead here to apply the conditions.See demo.
https://regex101.com/r/vV1wW6/22
$re = "/^(?=(?:.*[a-zA-Z]){2})(?=(?:.*[0-9]){4})\\w+$/m";
$str = "a1234b\nabcd1234\n4444aa\n12\n12a\n12aa22";
preg_match_all($re, $str, $matches);
You can use a human-readable check like this:
Remove all non-letters (\P{L} for letters and \P{M} for non-diacritics) and if the resulting string length is less than 2, return false
Remove all non-digits (\D) and if the resulting string length is less than 4, return false.
As for
it doesn't function if string start with a digit
You can add an if (preg_match('/^\D/', $str)) where ^\D means starts with a non-digit.
PHP code:
if (preg_match('/^\D/', $str)) {
if (mb_strlen(preg_replace('/\D/', '', $str), 'UTF8') < 4 ||
mb_strlen(preg_replace('/[\P{L}\P{M}]/u', '', $str), 'UTF8') < 2) {
echo "Invalid";
}
else
{
echo "Valid";
}
}
See IDEONE demo
Note that to count the length of a Unicode string you need mb_strlen function.
You can try this pattern (\b[a-zA-Z]+\d{4}[a-z]?|\b[a-zA-Z]+\d{4}|\b\d{4}[a-zA-Z]+)
See demo here https://regex101.com/r/uJ0vD4/8
Input
a1234b, abcd1234, 4444aa, 33333, ab555, aac3566
Output
a1234b, abcd1234, 4444aa, aac3566
^(?=(?:.*[0-9]){4})\w+$ works for this case
for PHP:
$re = "/^(?=(?:.*[0-9]){4})\\w+$/m";
$str = "a1234b\naaa32233\n";
preg_match_all($re, $str, $matches);
for javascript:
var re = /^(?=(?:.*[0-9]){4})\w+$/gm;
var str = 'a1234b\naaa32233\n';
var m;
while ((m = re.exec(str)) !== null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
how can I match a the first number next to a % character ?
<?php
$string = 'Get 30% off when you spend over £100 on electronics';
if(strpos($string,'% off') !== false) {
$number = preg_replace("/[^0-9%]/", '', $string);
return $number;
}
this returns 30%100
any help would be great thanks in advance.
This regex will match (and capture) all digits preceding a '%' sign:
'/(\d+)%/'
You can try something like this:
$string = 'Get 30% off when you spend over £100 on electronics';
preg_match('/(\d+)%/', $string, $matches);
print_r($matches[1]);
Do let us know if your requirements are more complex.
Regex:
\d{1,3}%
Explained:
\d{1,3} match a digit [0-9]
Quantifier: {1,3} Between 1 and 3 times, as many times as possible, giving back as needed [greedy]
% matches the character % literally
This seems to do the trick :)
if(strpos($string,'%') !== false) {
$regex_percent = "/((\d{1,5})(?:%))/";
preg_match($regex_percent, $string, $matches_off);
$number = $matches_off[2];
return $number;
}
I would like to know how to check with regex or whatever if a number begins with 0 or 00
I did so but I'm newbie at regex:
preg_match("/^[0][0-9]+$/", $number)
You may try the below code
if (preg_match("~^0\d+$~", $number)) {
// Yes
}
else {
// no
}
Explanation:
^ the beginning of the string
0 '0'
\d+ digits (0-9) (1 or more times)
$ before an optional \n, and the end of the
string
DEMO
^(?:0|00)\d+$
Try this.See demo,
http://regex101.com/r/rQ6mK9/16
This problem doesn't really require regex. Just use substr function:
if (substr($str, 0, 1) == '0' || substr($str, 0, 2) == '00') {
//=> matched
}
I would like to extract 16197226146 from the following string using PHP:
"(480) 710-6186" <18583894531.16197226146.S7KH51hwhM#txt.voice.google.com>
Could someone please help me with the regex please?
<\d*?\.(\d+)
< Match "<"
\d Match digits
* 0 or more times
? Lazy, take as little as possible
\. Match a "."
( Capture
\d Match digits
+ 1 or more times
) Stop capturing
That matches the second number after a .. The match is in group 1.
if (preg_match("/<\d*?\.(\d+)/", $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
You can play with the regex here.
You could use explode.
$value = "(480) 710-6186"<18583894531.16197226146.S7KH51hwhM#txt.voice.google.com>";
$result = explode('.', $value);
echo $result[1]; // is 16197226146
Is there a PHP function that grabs the string in between two characters. For example, I want to know what is between the percent and dollar symbol:
%HERE$
What is the function for this?
You could do that with a regex :
$string = 'test%HERE$glop';
$m = array();
if (preg_match('/%(.*?)\$/', $string, $m)) {
var_dump($m[1]);
}
Will get you :
string 'HERE' (length=4)
Couple of notes :
The $ in the pattern has the be escaped, as it has a special meaning (end of string)
You want to match everything : .*
that's between % and $
But in non-gready mode : .*?
And you want that captured -- hence the () arround it.
You can use a regular expression to get that:
preg_match('/%([^$]*)\\$/', $str, $match)
If a match was found, $match[0] will contain the whole match and $match[1] the text between % and $.
But you can also use basic string functions like strpos and search for the % and the first $ after that:
if (($start = strpos($str, '%')) !== false && ($end = strpos($str, '$', $start)) !== false) {
$match = substr($str, $start, $end-$start);
}