简体   繁体   English

正则表达式和 PHP - 如何匹配这个字符串?

[英]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) 4 位数字(点)
  • 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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