Tricky big data-processing problems (regular expressions)

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.