1

I'm trying to devise a regexp that extracts:

aa
bb
cc

from the subject:

aa,bb,cc

I'm using the following regexp:

|(.+?),*|

but the result is

a
a
b
b
c
c

Please help,

Thanks.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • If it is a simple comma delimited list then why not use explode() i.e $result = explode(",",$text); –  Jul 17 '14 at 15:00
  • Because the actual subject is this a prolog-generated list like this: [1-[],2-[3-a,4-b,5-a,7-b,8-d,9-e,10-f,11-g,12-h,13-i,14-l,15-a,16-m],3-[]] – Anton Maria Prati Jul 17 '14 at 15:02
  • does the string must be repeated or can be alphanumeric characters? – Federico Piazza Jul 17 '14 at 15:05
  • Given your response (that doens't match your original question) - what exactly are you expecting back from the data? –  Jul 17 '14 at 15:08

3 Answers3

3

Get the matched group from index 1.

(\w+),?

DEMO

Sample code:

$re = "/(\\w+),?/m";
$str = "aa,bb,cc";

preg_match_all($re, $str, $matches);

You can use PHP: Split string as well using explode:

$myArray = explode(',', $myString);

Read more How can I split a comma delimited string into an array in PHP?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
2

The ? makes your match 'non-greedy', that means it will match the shortest possible string that satisfies the regular expression. Also, ,* means 0 or more commas.

What you're looking for is:

|[^,]+|

For example:

<?php
$foo = "aa,bb,cc";
preg_match_all("/[^,]+/",$foo,$matches);
for($j=0;$j<count($matches[0]); $j++){
  print $matches[0][$j] .  "\n";
}
?>
terdon
  • 3,260
  • 5
  • 33
  • 57
1

Without any groups,

(?<=^|,)\w+

OR

\w+(?=,|$)

DEMO

PHP code would be,

<?php
$data = "aa,bb,cc";
$regex =  '~(?<=^|,)\w+~';
preg_match_all($regex, $data, $matches);
var_dump($matches);
?>

Output:

array(1) {
  [0]=>
  array(3) {
    [0]=>
    string(2) "aa"
    [1]=>
    string(2) "bb"
    [2]=>
    string(2) "cc"
  }
}
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274