I need help finding a PCRE pattern using preg_split().
I'm using the regex pattern below to split a string based on its starting 3 character code and semi-colons. The pattern works fine in Javascript, but now I need to use the pattern in PHP. I tried preg_split() but just getting back junk.
// Each group will begin with a three letter code, have three segments separated by a semi-colon. The string will not be terminated with a semi-colon.
// Pseudocode
string_to_split = "AAA;RED;111;BBB;BLUE;22;CCC;GREEN;33;DDD;WHITE;44"
// This works in JS
// https://regex101.com
$pattern = "/[AAA|BBB|CCC|DDD][^;]*;[^;]*[;][^;]*/gi";
Match 1
Full match 0-11 `AAA;RED;111`
Match 2
Full match 12-23 `BBB;BLUE;22`
Match 3
Full match 24-36 `CCC;GREEN;33`
Match 4
Full match 37-49 `DDD;WHITE;44`
$pattern = "/[AAA|BBB|CCC|DDD][^;]*;[^;]*[;][^;]*/";
$split = preg_split($pattern, $string_to_split);
returns
array(5)
0:""
1:";"
2:";"
3:";"
4:""
According to your additional information in some comments to the answers, I update my answer to be very specific to your source format.
You might want something like this:
$subject = "AAA;RED;111;AAA;Oh my dog;12.34;AAA;Oh Long John;.4556;BBB;Oh Long Johnson;1.2323;BBB;Oh Don Piano;.33;CCC;Why I eyes ya;1.445;CCC;All the live long day;2.3343;DDD;Faith Hilling;.89";
$pattern = '/(?<=;|^)(AAA|BBB|CCC|DDD);([^;]*);((?:\d*\.)?\d+)(?=;|$)/';
preg_match_all($pattern, $subject,$matches);
var_dump($matches);
giving you
array (size=4)
0 =>
array (size=8)
0 => string 'AAA;RED;111' (length=11)
1 => string 'AAA;Oh my dog;12.34' (length=19)
2 => string 'AAA;Oh Long John;.4556' (length=22)
3 => string 'BBB;Oh Long Johnson;1.2323' (length=26)
4 => string 'BBB;Oh Don Piano;.33' (length=20)
5 => string 'CCC;Why I eyes ya;1.445' (length=23)
6 => string 'CCC;All the live long day;2.3343' (length=32)
7 => string 'DDD;Faith Hilling;.89' (length=21)
1 =>
array (size=8)
0 => string 'AAA' (length=3)
1 => string 'AAA' (length=3)
2 => string 'AAA' (length=3)
3 => string 'BBB' (length=3)
4 => string 'BBB' (length=3)
5 => string 'CCC' (length=3)
6 => string 'CCC' (length=3)
7 => string 'DDD' (length=3)
2 =>
array (size=8)
0 => string 'RED' (length=3)
1 => string 'Oh my dog' (length=9)
2 => string 'Oh Long John' (length=12)
3 => string 'Oh Long Johnson' (length=15)
4 => string 'Oh Don Piano' (length=12)
5 => string 'Why I eyes ya' (length=13)
6 => string 'All the live long day' (length=21)
7 => string 'Faith Hilling' (length=13)
3 =>
array (size=8)
0 => string '111' (length=3)
1 => string '12.34' (length=5)
2 => string '.4556' (length=5)
3 => string '1.2323' (length=6)
4 => string '.33' (length=3)
5 => string '1.445' (length=5)
6 => string '2.3343' (length=6)
7 => string '.89' (length=3)
The start marker should occur at the start of string or immidiately after a semicolon, so we do a lookbehind, looking for start or semicolon:
(?<=;|^)
We look for an alternative of AAA,BBB,CCC or DDD and capture it:
(AAA|BBB|CCC|DDD)
After a semicolon we look for any character except a semicolon. The quantifier * means 0 or more time. Use + if you want at least 1.
;([^;]*)
After the next semicolon wie look for a number. This task has to be splitted to fit a valid format: We first look for 0 or more digits followed by a dot:
(?:\d*\.)?
where (?:) means a non-capturing group.
Behind we look for at least one digit: \d+
We want to capture both parts of of the number using parentheses after the searched semicolon:
;((?:\d*\.)?\d+)
This matches "1234", ".1234", "1.234", "12.34" , "123.4" but "1234.", "1.2.3"
Finally we want this to immediately occur before a semicolon or the end of string. Thus we do a lookahead:
(?=;|$)
Lookaheads and lookbehinds are not part of the captured result behind or respectively before.
I've modified your pattern a little, and added a couple of flags to preg_split.
The PREG_SPLIT_NO_EMPTY flag will exclude empty matches from the result, and PREG_SPLIT_DELIM_CAPTURE will include the captured value in the result.
$split = preg_split('/([abcd]{3};[^;]+;\d+);?/i', $string, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
Result:
Array
(
[0] => AAA;RED;111
[1] => BBB;BLUE;22
[2] => CCC;GREEN;33
[3] => DDD;WHITE;44
)
Alternatively, and more suitably, you can use preg_match_all with the following pattern.
preg_match_all('/([abcd]{3};[^;]+;\d+);?/i', $string, $matches);
print_r($matches[0]);
Result:
Array
(
[0] => AAA;RED;111
[1] => BBB;BLUE;22
[2] => CCC;GREEN;33
[3] => DDD;WHITE;44
)
You don't want to split your string but match elements, use preg_match_all:
$str = "AAA;RED;111;AAA;Oh my dog;2.34;AAA;Oh Long John;.4556;BBB;Oh Long Johnson;1.2323;BBB;Oh Don Piano;.33;CCC;Why I eyes ya;1.445;CCC;All the live long day;2.3343;DDD;Faith Hilling;.89";
$res = preg_match_all('/(?:AAA|BBB|CCC|DDD);[^;]*;[^;]*;?/', $str, $m);
print_r($m[0]);
Output:
Array
(
[0] => AAA;RED;111;
[1] => AAA;Oh my dog;2.34;
[2] => AAA;Oh Long John;.4556;
[3] => BBB;Oh Long Johnson;1.2323;
[4] => BBB;Oh Don Piano;.33;
[5] => CCC;Why I eyes ya;1.445;
[6] => CCC;All the live long day;2.3343;
[7] => DDD;Faith Hilling;.89
)
Explanation:
/ : regex delimiter
(?:AAA|BBB|CCC|DDD) : non capture group AAA or BBB or CCC or DDD
; : a semicolon
[^;]* : 0 or more any character that is not a semicolon
; : a semicolon
[^;]* : 0 or more any character that is not a semicolon
;? : optional semicolon
/ : regex delimiter
Considering this sample text:
grupo1, tiago1A, bola1A, mola1A, tijolo1A, pedro1B, bola1B, mola1B, tijolo1B, raimundo1C, bola1C, mola1C, tijolo1C, joao1D, bola1D, mola1D, tijolo1D, felipe1E, bola1E, mola1E, tijolo1E,
grupo2, tiago2A, bola2A, mola2A, tijolo2A, pedro2B, bola2B, mola2B, tijolo2B, raimundo2C, bola2C, mola2C, tijolo2C, joao2D, bola2D, mola2D, tijolo2D, felipe2E, bola2E, mola2E, tijolo2E,
grupo3, tiago3A, bola3A, mola3A, tijolo3A, pedro3B, bola3B, mola3B, tijolo3B, raimundo3C, bola3C, mola3C, tijolo3C, joao3D, bola3D, mola3D, tijolo3D, felipe3E, bola3E, mola3E, tijolo3E,
grupo4, tiago4A, bola4A, mola4A, tijolo4A, pedro4B, bola4B, mola4B, tijolo4B, raimundo4C, bola4C, mola4C, tijolo4C, joao4D, bola4D, mola4D, tijolo4D, felipe4E, bola4E, mola4E, tijolo4E,
grupo5, tiago5A, bola5A, mola5A, tijolo5A, pedro5B, bola5B, mola5B, tijolo5B, raimundo5C, bola5C, mola5C, tijolo5C, joao5D, bola5D, mola5D, tijolo5D, felipe5E, bola5E, mola5E, tijolo5E,
I would like to capture the 20 values that follow grupo3 and store them in groups of 4.
I am using this: (Demo)
/grupo3,((.*?),(.*?),(.*?),(.*?)),/
but this only returns the first 4 comma separated values after grupo3.
I need generate this array structure:
Match 1
Group 1 tiago3A
Group 2 bola3A
Group 3 mola3A
Group 4 tijolo3A
Match 2
Group 1 pedro3B
Group 2 bola3B
Group 3 mola3B
Group 4 tijolo3B
Match 3
Group 1 raimundo3C
Group 2 bola3C
Group 3 mola3C
Group 4 tijolo3C
Match 4
Group 1 joao3D
Group 2 bola3D
Group 3 mola3D
Group 4 tijolo3D
Match 5
Group 1 felipe3E
Group 2 bola3E
Group 3 mola3E
Group 4 tijolo3E
You can try the following:
/,(.*?),(.*?),(.*?),(.*?),.*?$/m
the /m in the end indicates the flag for multi-line and $ before that indicates end of line. Demo
Edit: For getting every 4 elements only form the 3rd paragraph
/grupo3,((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)),/
Demo
And you can get the desired output in PHP like:
preg_match('/grupo3,((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)), ((.*?),(.*?),(.*?),(.*?)),/', $str, $matches);
$groups = [];
unset($matches[0]);
$matches = array_values($matches);
$count = count($matches);
$j=0;
for($i=1;$i<$count;$i++)
{
if($i%5 == 0)
{
$j++;
continue;
}
$groups[$j][] = $matches[$i];
}
var_dump($groups);
Output will be something like:
array (size=5)
0 =>
array (size=4)
0 => string ' tiago3A' (length=8)
1 => string ' bola3A' (length=7)
2 => string ' mola3A' (length=7)
3 => string ' tijolo3A' (length=9)
1 =>
array (size=4)
0 => string 'pedro3B' (length=7)
1 => string ' bola3B' (length=7)
2 => string ' mola3B' (length=7)
3 => string ' tijolo3B' (length=9)
2 =>
array (size=4)
0 => string 'raimundo3C' (length=10)
1 => string ' bola3C' (length=7)
2 => string ' mola3C' (length=7)
3 => string ' tijolo3C' (length=9)
3 =>
array (size=4)
0 => string 'joao3D' (length=6)
1 => string ' bola3D' (length=7)
2 => string ' mola3D' (length=7)
3 => string ' tijolo3D' (length=9)
4 =>
array (size=4)
0 => string 'felipe3E' (length=8)
1 => string ' bola3E' (length=7)
2 => string ' mola3E' (length=7)
3 => string 'tijolo3E' (length=0)
Please forgive the lateness of this answer. This is the comprehensive answer with a clean/direct solution that I would have posted earlier if this page wasn't put on hold. This is as refined a solution as I can devise without knowing more about how your input data is generated/accessed.
The input:
$text='grupo1, tiago1A, bola1A, mola1A, tijolo1A, pedro1B, bola1B, mola1B, tijolo1B, raimundo1C, bola1C, mola1C, tijolo1C, joao1D, bola1D, mola1D, tijolo1D, felipe1E, bola1E, mola1E, tijolo1E,
grupo2, tiago2A, bola2A, mola2A, tijolo2A, pedro2B, bola2B, mola2B, tijolo2B, raimundo2C, bola2C, mola2C, tijolo2C, joao2D, bola2D, mola2D, tijolo2D, felipe2E, bola2E, mola2E, tijolo2E,
grupo3, tiago3A, bola3A, mola3A, tijolo3A, pedro3B, bola3B, mola3B, tijolo3B, raimundo3C, bola3C, mola3C, tijolo3C, joao3D, bola3D, mola3D, tijolo3D, felipe3E, bola3E, mola3E, tijolo3E,
grupo4, tiago4A, bola4A, mola4A, tijolo4A, pedro4B, bola4B, mola4B, tijolo4B, raimundo4C, bola4C, mola4C, tijolo4C, joao4D, bola4D, mola4D, tijolo4D, felipe4E, bola4E, mola4E, tijolo4E,
grupo5, tiago5A, bola5A, mola5A, tijolo5A, pedro5B, bola5B, mola5B, tijolo5B, raimundo5C, bola5C, mola5C, tijolo5C, joao5D, bola5D, mola5D, tijolo5D, felipe5E, bola5E, mola5E, tijolo5E,';
The method: (PHP Demo)
var_export(preg_match('/^grupo3, \K.*(?=,)/m',$text,$out)?array_chunk(explode(', ',$out[0]),4):'fail');
Use preg_match() to extract the single line, then use explode() to split the string on "comma space", then use array_chunk() to store in an array of 5 subarrays containing 4 elements each.
The pattern targets grupo3, at the start of the line, then restarts the full match using \K then greedily matches every non-newline character and stops just before the last comma in the line. The positive lookahead (?=,) doesn't store the final comma in the full string match.
(Pattern Demo)
My method does not retain any leading and trailing spaces, just the values themselves.
Output:
array (
0 =>
array (
0 => 'tiago3A',
1 => 'bola3A',
2 => 'mola3A',
3 => 'tijolo3A',
),
1 =>
array (
0 => 'pedro3B',
1 => 'bola3B',
2 => 'mola3B',
3 => 'tijolo3B',
),
2 =>
array (
0 => 'raimundo3C',
1 => 'bola3C',
2 => 'mola3C',
3 => 'tijolo3C',
),
3 =>
array (
0 => 'joao3D',
1 => 'bola3D',
2 => 'mola3D',
3 => 'tijolo3D',
),
4 =>
array (
0 => 'felipe3E',
1 => 'bola3E',
2 => 'mola3E',
3 => 'tijolo3E',
),
)
p.s. If the search term ($needle) is to be dynamic, you can use something like this to achieve the same result: (PHP Demo)
$needle='grupo3';
// if the needle may include any regex-sensitive characters, use preg_quote($needle,'/') at $needle
var_export(preg_match('/^'.$needle.', \K.*(?=,)/m',$text,$out)?array_chunk(explode(', ',$out[0]),4):'fail');
/* or this is equivalent...
if(preg_match('/^'.$needle.', \K.*(?=,)/m',$text,$out)){
$singles=explode(', ',$out[0]);
$groups=array_chunk($singles,4);
var_export($groups);
}else{
echo 'fail';
}
*/
Producing a range with PHP is easy when the range is something like 1 to 100 or A to Z. But I need to be able to produce ranges like 101A to 101Z or A1 to A100.
I thought that maybe PHP has a function to compare two strings, strip what's common between them and return the rest to form the range boundaries. However I can not find such a function. How would I achieve this?
EDIT: I don't have control over the format, I can only set the guidelines. The end user determines the pattern by entering something like A1-A100 into an input field.
101A to 101Z is like "101" + range("A", "Z")
and
A1 to A100 is like "A" + range(1, 100)
If you're looking for A1 to Z100 that's when things get a bit more complicated.
You can look at the way functions like base converters work, like dechex and base64_encode
If you're converting from the decimal system to your own notation you can do thing kind of conversion.
1 => A1
2 => A2
101 => B1
102 => B2
2601? => Z1
2700? => Z100
This just describes the outline. If you want code you'll have to make your question more clear.
Arbitrary ranges is .. very hard. I don't know of a solution. A1-A100 has a clear solution, but what about A1-100Z, how do you even begin? What about small-large or Boston-New York?
You can try with:
function rangeFix($from, $to, $prefix = null, $suffix = null) {
return array_map(function($item) use ($prefix, $suffix){
return $prefix . $item . $suffix;
}, range($from, $to));
}
rangeFix(0, 10, 'A', 'Z');
Output:
array (size=11)
0 => string 'A0Z' (length=3)
1 => string 'A1Z' (length=3)
2 => string 'A2Z' (length=3)
3 => string 'A3Z' (length=3)
4 => string 'A4Z' (length=3)
5 => string 'A5Z' (length=3)
6 => string 'A6Z' (length=3)
7 => string 'A7Z' (length=3)
8 => string 'A8Z' (length=3)
9 => string 'A9Z' (length=3)
10 => string 'A10Z' (length=4)
or:
rangeFix('A', 'Z', 101);
Output:
array (size=26)
0 => string '101A' (length=4)
1 => string '101B' (length=4)
2 => string '101C' (length=4)
3 => string '101D' (length=4)
4 => string '101E' (length=4)
5 => string '101F' (length=4)
6 => string '101G' (length=4)
7 => string '101H' (length=4)
8 => string '101I' (length=4)
9 => string '101J' (length=4)
10 => string '101K' (length=4)
11 => string '101L' (length=4)
12 => string '101M' (length=4)
13 => string '101N' (length=4)
14 => string '101O' (length=4)
15 => string '101P' (length=4)
16 => string '101Q' (length=4)
17 => string '101R' (length=4)
18 => string '101S' (length=4)
19 => string '101T' (length=4)
20 => string '101U' (length=4)
21 => string '101V' (length=4)
22 => string '101W' (length=4)
23 => string '101X' (length=4)
24 => string '101Y' (length=4)
25 => string '101Z' (length=4)
This is my own, not so elegant solution. It works with the following logic:
Range boundaries have two parts, of which the other is digit and the other non-digit, such as A1 or 1A. One-part string work too, such as A or 1. I did not test with strings such as A1B where there are more than two parts. The script probably fails there.
$s1 = '101A';
$s2 = '101Z';
$s1_d = preg_split('/\d+/', $s1);
$s1_D = preg_split('/\D+/', $s1);
$s2_d = preg_split('/\d+/', $s2);
$s2_D = preg_split('/\D+/', $s2);
if($s1_d[0] == '') $s1_d[0] = $s1_D[0];
else $s1_d[1] = $s1_D[1];
$s1 = $s1_d;
if($s2_d[0] == '') $s2_d[0] = $s2_D[0];
else $s2_d[1] = $s2_D[1];
$s2 = $s2_d;
$prefix = false;
$postfix = false;
if($s1[0] == $s2[1]) die(); // Can't do it.
if($s1[0] == $s2[0]) {
$prefix = $s1[0];
$start = $s1[1];
$end = $s2[1];
}
else {
$postfix = $s1[1];
$start = $s1[0];
$end = $s2[0];
}
$range = range($start, $end);
foreach($range as &$r) {
$r = $prefix . $r . $postfix;
}
var_dump($range);