Forum Moderators: phranque
i want to write a pattern that matches all occurrences of an alphabet from a-c and then a digit (1-2) i.e a set of
a1 b1 c2, a1 b1, a1 c2, b1 c2, a1,b1,c2
I thought to write something like
preg_match("/^(a-c)\d\s(a-c)\d\s(a-c)\d/",$str,$matches);
but this logic does not work at all.
any idea?
thank you
preg_match("/^[a-c][12]/",$str,$matches);
Jim
/(\s[a-c\d]\s)+/
or
/(\s?[a-c\d]\s?)+/
in my explanation,
preceded or ended on a whitespace character with one alphabet from a to c followed by a digit, with one or more occurrences.
therefore i thought to post it here and see if some experienced person can give me the exact regex :) because i already tried many things with no luck, now i am curious to see the actual working regex for this.
/\s?([a-c]\d\s?)+/
Jim
I have been giving the test string in my posts. I used your suggested pattern as follow
<?php
$str = "a1sd b2fd c2kj"; //testing on this string
$regex = '/\s?([a-c]\d\s?)+/';
preg_match($regex,$str,$matches);
echo "<pre>"; print_r($matches);
?>
now it gives me following output
Array
(
[0] => a1
[1] => a1
)
EDIT: the same pattern, surprisingly, gives me following array on the online RE tester,
a1
b2
c2
still not what i am looking for. i want all the combination set of alphabet-followed by a digit, in all combinations as explained in my very first post.
$regex = '/(\s?([a-c]\d\s?)+)/';
Jim