首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP: regex,preg_split

PHP: regex,preg_split
EN

Stack Overflow用户
提问于 2015-07-06 02:48:01
回答 2查看 87关注 0票数 2

我有一个字符串,它有这样的内容。

代码语言:javascript
复制
19. Which of the following conflicting criteria does the problem below satisfe. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
a.  Writability vs Readability
b.  Reliability vs Cost of execution
c.  Writability vs Reliability
d.  Cost of execution vs. Readability
e.  Cost of execution vs. Readability

我想要做的是像这样把它分开。

代码语言:javascript
复制
    [0] => 19.  Which of the following conflicting criteria does the problem below satisfye. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.

    [1] => a.   Writability vs Readability

    [2] => b.   Reliability vs Cost of execution

    [3] => c.   Writability vs Reliability

    [4] => d.   Cost of execution vs. Readability

    [5] => e.   Cost of execution vs. Readability

我的判断力很弱,我有这样的结果。

代码语言:javascript
复制
preg_split('/(?=[a-e\d]+\.(?!\d))/', $entries, -1, PREG_SPLIT_NO_EMPTY);

    [0] => 1
    [1] => 9.   Which of the following conflicting criteria does the problem below satisfy
    [2] => e. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.

    [3] => a.   Writability vs Readability

    [4] => b.   Reliability vs Cost of execution

    [5] => c.   Writability vs Reliability

    [6] => d.   Cost of execution vs. Readability

    [7] => e.   Cost of execution vs. Readability

我该怎么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-06 04:52:33

据我所知,如果有^[a-e\d]+\. 在前面 (启动下一行),您希望在一个或多个垂直空间拆分。拆分函数很好:

代码语言:javascript
复制
$pattern = '/\v+(?=^[a-e\d]+\.)/m';

m是用于使^插入符号匹配行开始(不仅仅是字符串开始)的多行标志。

代码语言:javascript
复制
print_r(preg_split($pattern, $str));

eval.in测试;应该给出所需的结果:

代码语言:javascript
复制
Array
(
    [0] => 19. Which of the following conflicting criteria does the problem below satisfe. 2.1
C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
    [1] => a.  Writability vs Readability
    [2] => b.  Reliability vs Cost of execution
    [3] => c.  Writability vs Reliability
    [4] => d.  Cost of execution vs. Readability
    [5] => e.  Cost of execution vs. Readability
)

也是用于测试拆分序列的请参阅regex101。如果空行之间有空格,请尝试\s+ (任何空格中的一个或多个),而不是\v+

票数 1
EN

Stack Overflow用户

发布于 2015-07-06 02:59:02

如果你只想打破每一条线..。

代码语言:javascript
复制
$str = "19. Which of the following conflicting criteria does the problem below satisfe. 2.1 C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
a.  Writability vs Readability
b.  Reliability vs Cost of execution
c.  Writability vs Reliability
d.  Cost of execution vs. Readability
";

preg_match_all('/(.*)\n/', $str, $matches);

var_dump($matches);

会给你

代码语言:javascript
复制
array(2) {
    [0]=>
    array(5) {
        [0]=>
        string(169) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1    C++ pointers are powerful and very flexible but at the cost of poor intelligibility.
        "
        [1]=>
        string(32) "a.  Writability vs Readability
        "
        [2]=>
        string(38) "b.  Reliability vs Cost of execution
        "
        [3]=>
        string(32) "c.  Writability vs Reliability
        "
        [4]=>
        string(39) "d.  Cost of execution vs. Readability
        "
    }
    [1]=>
    array(5) {
        [0]=>
        string(168) "19. Which of the following conflicting criteria does the problem below satisfe. 2.1    C++ pointers are powerful and very flexible but at the cost of poor intelligibility."
        [1]=>
        string(31) "a.  Writability vs Readability"
        [2]=>
        string(37) "b.  Reliability vs Cost of execution"
        [3]=>
        string(31) "c.  Writability vs Reliability"
        [4]=>
        string(38) "d.  Cost of execution vs. Readability"
    }
}

preg_match_all()将使您能够在多行上匹配模式,而$matches数组将为您提供所有匹配。因为它将整行(包括\n)作为匹配处理,所以这是第一个数组。因此,您需要由(.*)匹配的部分,它位于$matches数组的第二个元素中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31237132

复制
相关文章

相似问题

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