简体   繁体   中英

Regex and PHP - how to match this string?

I have this string:

First part of string. Second part one: Second part two; Third part: Fourth part 2014. Available online:

The string is made in this way:

  • First part of string (dot)
  • Second part one (colon)
  • Second part two (semicolon)
  • Third part (colon)
  • Fourth part
  • 4 digit number (dot)
  • Available online (colon)

How it is possible to split this string in an array with as described, using regex?

Thank you.

You probably want something like:

$array = preg_split('/\s*[:;.]\s*/', $str);

or even:

$array = preg_split('/\s*[:;.]\s*/', $str, -1, PREG_SPLIT_NO_EMPTY);
$string = "First part of string. Second part one: Second part two; Third part: Fourth part 2014. Available online:";
preg_match("/(.+?)\.(.+?):(.+?);(.+?):(.+?)(\d{4})\.(.+?):/",$string,$m);
print_r($m);

outputs:

Array
(
    [0] => First part of string. Second part one: Second part two; Third part: Fourth part 2014. Available online:
    [1] => First part of string
    [2] =>  Second part one
    [3] =>  Second part two
    [4] =>  Third part
    [5] =>  Fourth part 
    [6] => 2014
    [7] =>  Available online
)

Hope it'd help you.

Using:

([a-zA-Z ]+)\. ([a-zA-Z ]+): ([a-zA-Z ]+); ([a-zA-Z ]+): ([a-zA-Z ]+)([0-9]{4})\. ([a-zA-Z ]+):

Results in:

First part of string
Second part one
Second part two
Third part
Fourth part 
2014
Available online

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM