Some time ago, I collected more than 40,000 wallpaper data, format similar to the following:
-------------------a
1
2
3
-------------------b
AK
al
am
an
ao
AP
aq
ar
-------------------C
1
/
71
Because to guide the MySQL database, I want the final format is this:
A-1
a-2
a-3
b-ak
b-al
b-am
b-an b-ao b-ap b-aq b-ar c-1
c-11
c-21
c-31
c-41
c-51
c-61
With Sublime Text3 the regular, and even the recursion has learned in sublime text3 or can not be achieved.
(doing, killing people, even with Excel is trouble.) )
Finally I wrote a PHP to deal with this thing:
<?php
$headPattern = '-* (\w) '; Match head
$childPattern = ' \ r \ n (\d+|\w+) '; Match body
$pattern = "/{$headPattern} ((?: $childPattern) (? 2) *)/";
$pattern = "/-* (\w) (?: \ r\n (\d+|\w+)) (? 2) *)/";
$subject = "
-------------------a
1
2
3
4
5
6 7 8------------ -------b
AK
al
am
an
ao
ap
aq
ar
-------------------c
1 One by one
;
Echo preg_replace_callback ($pattern, function ($e) use ($childPattern) {
//two-time regular match
preg_match_all ('/'. $ Childpattern. '/', $e [2], $match);
$result = ';
foreach ($match [1] as $key => $value) {
$result. = $e [1]. '-' . $value. "\ n";
}
return $result;
}, $subject);
Do not parse, look at the code.