Hello, I’m writing a regex to use in preg_match_all, it’s been successful for preg_match, but I can’t get it right for preg_match_all. The pattern: [T-xxx-yyy] with xxx as numbers (unlimited length) and yyy as alphabet (unlimited length). For example: [T-123-OK] or [T-8291-CANCEL] will match the pattern.
Um sorry I made a mistake for the original regex, I just retype it in a hurry instead of copying from my php file, hence the unescaped [. What I actually used:
$regex = "/(.*)(\\[T-[0-9]+-[A-Za-z]+\\])(.*)/";
Anyway, I tried Jake’s code and it works Although I’m curious of the
(\\d+?)
Why do we need the “?”
hash, I tried your code on a multiline and it only return the last match of each line.
Hmm I’m not sure why, but I tested it with this code. Anyway, thanks for your help! Really great community, I asked this question in other board yesterday and I haven’t got any replies yet!
$regex = "~\\[T-\\d+-[A-Z]+\\]~";
$matches = array();
$text13 = "MY TASK [T-122-done] is DONE,
MY TASK [T-134-DONE] is DONE,
MY TASK [T-253-Done] is DONE,
MY TASK [T-321-Done] is DONE,
MY TASK [T-654-DONE] is DONE";
preg_match_all($regex, $text13,$matches,PREG_PATTERN_ORDER);
echo "<pre>";
print_r($matches);
echo "</pre>";
/*
Array
(
[0] => Array
(
[0] => [T-134-DONE]
[1] => [T-654-DONE]
)
)
*/
With the code in your last post, the “yyy” part is only matching uppercase letters but some include lowercase letters (“Done”). Either change the character class (“[…]”) to allow lowercase letters or make the entire regular express case-insensitive by using the “i” modifier (“…~i”)