preg_match_all 按字母顺序排列的列表字符串,例如A. {文本)。 B. {文本}等

preg_match_all alphabetical list string e.g. A. {text). B. {text} etc

我正在尝试找到一种方法,通过首先匹配“A.”、“B.”、“C.”来从字符串中提取项目。等等,在下一个之前获取每个值,例如获得“A”。一直到下一个 B. 然后从 B. 到下一个 C. 如果每个都存在的话。

示例字符串 1:

A. What sort of country do the framers of the Northwest Ordinance envision for the next generation of Americans?
B. How do the statements on behalf of individual religious rights and the public support of religion compare with the statements found in the new state constitutions?

在上面的简单字符串中,我终于设法使用了preg_match_all('/(.*?)\?/', $aString, $matches);。这只是在尝试理解正则表达式的每个部分并检查所有其他尽可能接近我的 Whosebug 问题之后。此 preg_match_all 代码似乎在第二项的前导 space 之外有效:

array (size=2)
  0 => 
    array (size=2)
      0 => string 'A. What sort of country do the framers of the Northwest Ordinance envision for the next generation of Americans?' (length=112)
      1 => string ' B. How do the statements on behalf of individual religious rights and the public support of religion compare with the statements found in the new state constitutions?' (length=167)
  1 => 
    array (size=2)
      0 => string 'A. What sort of country do the framers of the Northwest Ordinance envision for the next generation of Americans' (length=111)
      1 => string ' B. How do the statements on behalf of individual religious rights and the public support of religion compare with the statements found in the new state constitutions' (length=166)

示例字符串 2:

A. What explains President Lincoln’s attitude toward Louisiana in his letter to General Banks? Does his Second Inaugural Address explain his attitude? How do Lincoln, Douglass, and Stevens’ attitudes toward the South differ? Is Stevens’ constitutional argument about the basis of Reconstruction sound? If so, was that sufficient to make his approach to the seceded states sound? Do Stevens’ remarks about Jews, the Irish and others undermine his claim to be a champion of the principles of the Declaration of Independence? Was the response of Southerners as described and defended by Tillman inevitable, or could some version of restoration or reconstruction have prevented it?
B. Do the views expressed in the twentieth century differ from those expressed in the documents below? For example, compare the views of Senators Tillman and Thurmond, both Democrats from South Carolina. Did the constitutional arguments change between the 1860s and the 1960s?
C. How true does President Abraham Lincoln’s remark in his Second Inaugural Address that both Northerners and Southerners prayed to the same God and read the same Bible appear in light of the very different interpretations of said Bible on the question of slavery, as evidenced in the antebellum period?

问题在于,在第二个示例中,我不能只尝试获取以“?”结尾的每个字符串,因为“A”。包含多个带有“?”的项目这应该是第一次提取的一部分,直到下一个“B”。然后是“C”。等等。我想要得到的结果是这样的:

array (size=3)
   0 => string 'A. What explains President Lincoln’s attitude toward Louisiana in his letter to General Banks? Does his Second Inaugural Address explain his attitude? How do Lincoln, Douglass, and Stevens’ attitudes toward the South differ? Is Stevens’ constitutional argument about the basis of Reconstruction sound? If so, was that sufficient to make his approach to the seceded states sound? Do Stevens’ remarks about Jews, the Irish and others undermine his claim to be a champion of the principles of the Declaration of Independence? Was the response of Southerners as described and defended by Tillman inevitable, or could some version of restoration or reconstruction have prevented it?'
   1 => string 'B. Do the views expressed in the twentieth century differ from those expressed in the documents below? For example, compare the views of Senators Tillman and Thurmond, both Democrats from South Carolina. Did the constitutional arguments change between the 1860s and the 1960s?'
   2 => string 'C. How true does President Abraham Lincoln’s remark in his Second Inaugural Address that both Northerners and Southerners prayed to the same God and read the same Bible appear in light of the very different interpretations of said Bible on the question of slavery, as evidenced in the antebellum period?'

我要循环到 运行 的字符串数组的总大小是 813。示例 1 和示例 2 之间的每一个都不同。它们的共同点是每个问题都以一个“A.”,那么如果字符串中有另一个问题,它将是“B”。等等。我的另一个问题是,如果服务器没有抛出资源限制错误或其他问题,这是否可行。

你可以使用

(?sm)\b[A-Z]\..*?\?(?=\h+[A-Z]\.|$)
  • (?sm) 内联修饰符,点匹配换行符并启用多行
  • \b[A-Z]\. 防止部分匹配的单词边界,匹配大写字符 A-Z 和 .
  • .*?\? 匹配尽可能少的字符,直到第一个 ?
  • (?=\h+[A-Z]\.|$) 正面前瞻,断言右边是 1+ 水平空白字符后跟大写字符和 . 或字符串结尾

Regex demo | Php demo

例如

$re = '/\b[A-Z]\..*?\?(?=\h+[A-Z]\.|$)/sm';
$str = 'A. What explains President Lincoln’s attitude toward Louisiana in his letter to General Banks? Does his Second Inaugural Address explain his attitude? How do Lincoln, Douglass, and Stevens’ attitudes toward the South differ? Is Stevens’ constitutional argument about the basis of Reconstruction sound? If so, was that sufficient to make his approach to the seceded states sound? Do Stevens’ remarks about Jews, the Irish and others undermine his claim to be a champion of the principles of the Declaration of Independence? Was the response of Southerners as described and defended by Tillman inevitable, or could some version of restoration or reconstruction have prevented it?
B. Do the views expressed in the twentieth century differ from those expressed in the documents below? For example, compare the views of Senators Tillman and Thurmond, both Democrats from South Carolina. Did the constitutional arguments change between the 1860s and the 1960s?
C. How true does President Abraham Lincoln’s remark in his Second Inaugural Address that both Northerners and Southerners prayed to the same God and read the same Bible appear in light of the very different interpretations of said Bible on the question of slavery, as evidenced in the antebellum period?';

preg_match_all($re, $str, $matches);

print_r($matches[0]);

输出

Array
(
    [0] => A. What explains President Lincoln’s attitude toward Louisiana in his letter to General Banks? Does his Second Inaugural Address explain his attitude? How do Lincoln, Douglass, and Stevens’ attitudes toward the South differ? Is Stevens’ constitutional argument about the basis of Reconstruction sound? If so, was that sufficient to make his approach to the seceded states sound? Do Stevens’ remarks about Jews, the Irish and others undermine his claim to be a champion of the principles of the Declaration of Independence? Was the response of Southerners as described and defended by Tillman inevitable, or could some version of restoration or reconstruction have prevented it?
    [1] => B. Do the views expressed in the twentieth century differ from those expressed in the documents below? For example, compare the views of Senators Tillman and Thurmond, both Democrats from South Carolina. Did the constitutional arguments change between the 1860s and the 1960s?
    [2] => C. How true does President Abraham Lincoln’s remark in his Second Inaugural Address that both Northerners and Southerners prayed to the same God and read the same Bible appear in light of the very different interpretations of said Bible on the question of slavery, as evidenced in the antebellum period?
)