我已经在这里工作了几个小时,这里的解决方案都没有真正帮助我。我有一个格式为" NN的文本文件:NN字符串在这里"。实际文件如下。我需要正则表达章节:诗歌来自实际的弦乐诗。如您所见,并非所有内容都由换行符分隔。我最接近的是(\d{1,2}:\d{1,2})[^\d]*
,但它只是真正区分NN:NN。
如何完成字符串的分离?
1:1 The book of the generation of Jesus Christ, the son of David, the son of Abraham.
1:2 Abraham begat Isaac; and Isaac begat Jacob; and Jacob begat Judas and his brethren; 1:3 And Judas begat Phares and Zara of Thamar; and Phares begat Esrom; and Esrom begat Aram; 1:4 And Aram begat Aminadab; and Aminadab begat Naasson; and Naasson begat Salmon; 1:5 And Salmon begat Booz of Rachab; and Booz begat Obed of Ruth; and Obed begat Jesse; 1:6 And Jesse begat David the king; and David the king begat Solomon of her that had been the wife of Urias; 1:7 And Solomon begat Roboam; and Roboam begat Abia; and Abia begat Asa; 1:8 And Asa begat Josaphat; and Josaphat begat Joram; and Joram begat Ozias; 1:9 And Ozias begat Joatham; and Joatham begat Achaz; and Achaz begat Ezekias; 1:10 And Ezekias begat Manasses; and Manasses begat Amon; and Amon begat Josias; 1:11
And Josias begat Jechonias and his brethren, about the time they were carried away to Babylon: 1:12 And after they were brought to Babylon, Jechonias begat Salathiel; and Salathiel begat Zorobabel; 1:13 And Zorobabel begat Abiud; and Abiud begat Eliakim; and Eliakim begat Azor; 1:14 And Azor begat Sadoc; and Sadoc begat Achim; and Achim begat Eliud; 1:15 And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob; 1:16 And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
1:17 So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.
1:18 Now the birth of Jesus Christ was on this wise: When as his mother Mary was espoused to Joseph, before they came together, she was found with child of the Holy Ghost.
答案 0 :(得分:1)
你很亲密。以下应该有效:
preg_match_all("/(\d{1,2}:\d{1,2})([^\d]*)/", $str, $output_array);
print_r(array_combine($output_array[1], $output_array[2]));
http://sandbox.onlinephpfunctions.com/code/e5522443d16558890431519ec6dd03a308ca1e32
答案 1 :(得分:1)
正则表达式:(\d+:\d+)\R?\s*(.+?(?=\s*\d+:\d+|$))
<强>详情:
dp(2)
匹配一个数字(等于\d
)[0-9]
匹配任何Unicode换行序列\R
匹配任何空格字符\s
匹配任何字符(行终止符除外).+?
断言位于字符串末尾的位置$
匹配0到1次?
或|
匹配一次且无限次+
零和无限次之间的匹配PHP代码:
*
<强>输出强>:
$text = "1:1 The book of the generation of Jesus Christ, the son of David, the son of Abraham............";
preg_match_all("/(\d+:\d+)\R?\s*(.+?(?=\s*\d+:\d+|$))/m", $text, $matches);
print_r(array_combine($matches[1], $matches[2]));
答案 2 :(得分:0)
这不仅速度相当快,还会修剪文本值中的所有前导/尾随空格。 *您的所有文字行都以:
,;
或.
结尾。我正在利用这一事实来提高模式效率。
如果在您的真实项目中,某些句子包含换行符(您的示例没有),则在第二个模式分隔符之后添加s
,以使.
匹配换行符。
~(\d{1,2}:\d{1,2})\s+(.*?[:;.](?=\s*(?:\d{1,2}:\d{1,2})|$))~
2193steps
代码:(Demo)
$text="1:1 The book of the generation of Jesus Christ, the son of David, the son of Abraham.
1:2 Abraham begat Isaac; and Isaac begat Jacob; and Jacob begat Judas and his brethren; 1:3 And Judas begat Phares and Zara of Thamar; and Phares begat Esrom; and Esrom begat Aram; 1:4 And Aram begat Aminadab; and Aminadab begat Naasson; and Naasson begat Salmon; 1:5 And Salmon begat Booz of Rachab; and Booz begat Obed of Ruth; and Obed begat Jesse; 1:6 And Jesse begat David the king; and David the king begat Solomon of her that had been the wife of Urias; 1:7 And Solomon begat Roboam; and Roboam begat Abia; and Abia begat Asa; 1:8 And Asa begat Josaphat; and Josaphat begat Joram; and Joram begat Ozias; 1:9 And Ozias begat Joatham; and Joatham begat Achaz; and Achaz begat Ezekias; 1:10 And Ezekias begat Manasses; and Manasses begat Amon; and Amon begat Josias; 1:11
And Josias begat Jechonias and his brethren, about the time they were carried away to Babylon: 1:12 And after they were brought to Babylon, Jechonias begat Salathiel; and Salathiel begat Zorobabel; 1:13 And Zorobabel begat Abiud; and Abiud begat Eliakim; and Eliakim begat Azor; 1:14 And Azor begat Sadoc; and Sadoc begat Achim; and Achim begat Eliud; 1:15 And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob; 1:16 And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.
1:17 So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.
1:18 Now the birth of Jesus Christ was on this wise: When as his mother Mary was espoused to Joseph, before they came together, she was found with child of the Holy Ghost.";
$pattern='/(\d{1,2}:\d{1,2})\s+(.*?[:;.](?=\s*(?:\d{1,2}:\d{1,2})|$))/';
var_export(preg_match_all($pattern,$text,$out)?array_combine($out[1],$out[2]):[]);
输出:
array (
'1:1' => 'The book of the generation of Jesus Christ, the son of David, the son of Abraham.',
'1:2' => 'Abraham begat Isaac; and Isaac begat Jacob; and Jacob begat Judas and his brethren;',
'1:3' => 'And Judas begat Phares and Zara of Thamar; and Phares begat Esrom; and Esrom begat Aram;',
'1:4' => 'And Aram begat Aminadab; and Aminadab begat Naasson; and Naasson begat Salmon;',
'1:5' => 'And Salmon begat Booz of Rachab; and Booz begat Obed of Ruth; and Obed begat Jesse;',
'1:6' => 'And Jesse begat David the king; and David the king begat Solomon of her that had been the wife of Urias;',
'1:7' => 'And Solomon begat Roboam; and Roboam begat Abia; and Abia begat Asa;',
'1:8' => 'And Asa begat Josaphat; and Josaphat begat Joram; and Joram begat Ozias;',
'1:9' => 'And Ozias begat Joatham; and Joatham begat Achaz; and Achaz begat Ezekias;',
'1:10' => 'And Ezekias begat Manasses; and Manasses begat Amon; and Amon begat Josias;',
'1:11' => 'And Josias begat Jechonias and his brethren, about the time they were carried away to Babylon:',
'1:12' => 'And after they were brought to Babylon, Jechonias begat Salathiel; and Salathiel begat Zorobabel;',
'1:13' => 'And Zorobabel begat Abiud; and Abiud begat Eliakim; and Eliakim begat Azor;',
'1:14' => 'And Azor begat Sadoc; and Sadoc begat Achim; and Achim begat Eliud;',
'1:15' => 'And Eliud begat Eleazar; and Eleazar begat Matthan; and Matthan begat Jacob;',
'1:16' => 'And Jacob begat Joseph the husband of Mary, of whom was born Jesus, who is called Christ.',
'1:17' => 'So all the generations from Abraham to David are fourteen generations; and from David until the carrying away into Babylon are fourteen generations; and from the carrying away into Babylon unto Christ are fourteen generations.',
'1:18' => 'Now the birth of Jesus Christ was on this wise: When as his mother Mary was espoused to Joseph, before they came together, she was found with child of the Holy Ghost.',
)
说明:
~ #Pattern delimiter
(\d{1,2}:\d{1,2}) #Capture nn:nn as Group1
\s+ #Match one or more whitespaces (including newlines)
( #Start Capture Group2
.*? #Lazily match zero or more non-newline characters
[:;.] #Match a colon, semi-colon, or dot
(?= #Start "lookahead" (aka: match but don't consume)
\s* #Match zero or more whitespace characters
(?:\d{1,2}:\d{1,2}) #Match nn:nn
| #Or
$ #Match the end of the entire string
) #End "lookahead"
) #End Capture Group2
~ #Pattern delimiter
答案 3 :(得分:-1)