PHP preg_match圣经经文格式

时间:2012-04-02 09:34:14

标签: php regex preg-match

我正在努力构建一个正则表达式来解析这种字符串(圣经经文):

  'John 14:16–17, 25–26'
  'John 14:16–17'
  'John 14:16'
  'John 14'
  'John'

所以基本模式是:

Book [[Chapter][:Verse]]

其中章和节是可选的。

5 个答案:

答案 0 :(得分:9)

我认为这可以满足您的需求:

\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?

假设:

  • 数字始终为1位或2位
  • 该短划线将与以下-
  • 中的任何一个匹配

以下是带注释的正则表达式:

"
\w         # Match a single character that is a “word character” (letters, digits, and underscores)
   +          # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\s         # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   ?          # Between zero and one times, as many times as possible, giving back as needed (greedy)
(          # Match the regular expression below and capture its match into backreference number 1
   \d         # Match a single digit 0..9
      {1,2}      # Between one and 2 times, as many times as possible, giving back as needed (greedy)
)?         # Between zero and one times, as many times as possible, giving back as needed (greedy)
(          # Match the regular expression below and capture its match into backreference number 2
   :          # Match the character “:” literally
   \d         # Match a single digit 0..9
      {1,2}      # Between one and 2 times, as many times as possible, giving back as needed (greedy)
)?         # Between zero and one times, as many times as possible, giving back as needed (greedy)
(          # Match the regular expression below and capture its match into backreference number 3
   [-–]       # Match a single character present in the list “-–”
   \d         # Match a single digit 0..9
      {1,2}      # Between one and 2 times, as many times as possible, giving back as needed (greedy)
)?         # Between zero and one times, as many times as possible, giving back as needed (greedy)
(          # Match the regular expression below and capture its match into backreference number 4
   ,          # Match the character “,” literally
   \s         # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   \d         # Match a single digit 0..9
      {1,2}      # Between one and 2 times, as many times as possible, giving back as needed (greedy)
   [-–]       # Match a single character present in the list “-–”
   \d         # Match a single digit 0..9
      {1,2}      # Between one and 2 times, as many times as possible, giving back as needed (greedy)
)?         # Between zero and one times, as many times as possible, giving back as needed (greedy)
"

以下是一些在php中使用的例子:

if (preg_match('/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?/', $subject)) {
    # Successful match
} else {
    # Match attempt failed
}

获取给定字符串中所有匹配项的数组

preg_match_all('/\w+\s?(\d{1,2})?(:\d{1,2})?([-–]\d{1,2})?(,\s\d{1,2}[-–]\d{1,2})?/', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];

答案 1 :(得分:4)

在这里试试

\b[a-zA-Z]+(?:\s+\d+)?(?::\d+(?:–\d+)?(?:,\s*\d+(?:–\d+)?)*)?

查看并测试here on Regexr

由于末尾有(?:,\s*\d+(?:–\d+)?)*,你可以在结尾处列出经文,经文范围。

答案 2 :(得分:3)

使用此正则表达式:

[A-Za-z]+( ([0-9]+)(:[0-9]+)?([\-–][0-9]+)?(, [0-9]+[\-–][0-9]+)?)?

或其“更漂亮”的版本:

\w+( (\d+)(:\d+)?([\-–]\d+)?(, \d+[\-–]\d+)?)?

更新:匹配短划线或连字符


注意: 我已对其进行了测试,并且它与所有5种可能的版本相匹配。

示例:http://regexr.com?30h4q

enter image description here

答案 3 :(得分:0)

([1|2|3]?([i|I]+)?(\s?)\w+(\s+?))((\d+)?(,?)(\s?)(\d+))+(:?)((\d+)?([\-–]\d+)?(,(\s?)\d+[\-–]\d+)?)?

适用于几乎所有的书......

答案 4 :(得分:0)

   (\b[a-zA-Z]\w+\s\d+)(:\d+)+([-–]\d+)?([,;](\s)?(\d+:)?\d+([-–]\d+)?)?

这是此处提供的所有代码的混合。它不会突出显示的唯一格式是" 仅限图书名称"或" book&仅限章节" (只需添加" :1-all "在#章之后)我发现提供的其他代码可以限定太多的变体,不符合圣经经文的语法。

以下是我在RegExr中测试过的示例:(暂时无法发布图片)

  

John humbolt 14:16-17,25-26
  约翰福音14:16-17
  约翰福音14:16
  约翰福音77:3; 2:9-11
   John 5:1-all brad 555-783-6867
   John 6
  你好,你怎么样
  以斯拉记32:5 约翰福音14:16-17,25-36
   12 23:34
  约翰福音14:16-17,25-36
  约翰福音14:16-17; 32:25