簡體   English   中英

正則表達式和 PHP - 如何匹配這個字符串?

[英]Regex and PHP - how to match this string?

我有這個字符串:

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

字符串是這樣制作的:

  • 字符串的第一部分(點)
  • 第二部分一(冒號)
  • 第二部分二(分號)
  • 第三部分(冒號)
  • 第四部分
  • 4 位數字(點)
  • 在線提供(冒號)

如何使用正則表達式將這個字符串拆分為數組中的描述?

謝謝你。

你可能想要這樣的東西:

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

甚至:

$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);

輸出:

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
)

希望對你有幫助。

使用:

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

結果是:

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