首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >preg_match_all按字母顺序排列的列表字符串,例如A. {text。B. {文本}等

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

Stack Overflow用户
提问于 2021-09-08 11:06:43
回答 1查看 63关注 0票数 0

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

示例字符串1:

代码语言:javascript
复制
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);。这是在试图理解正则表达式的每个部分并检查所有其他尽可能接近我的堆栈溢出问题之后。在第二项中,这段preg_match_all代码似乎比前面的空间更有效:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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.“诸若此类。我想得到的结果是:

代码语言:javascript
复制
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”。诸若此类。我关心的另一个问题是,如果没有服务器抛出资源限制错误或其他东西,这是可能的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-08 11:08:47

你可以用

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

Regex演示 x- Php演示

例如

代码语言:javascript
复制
$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]);

输出

代码语言:javascript
复制
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?
)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69101896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档