用于解析斜体文本的正则表达式?

发布于 2024-10-15 16:13:48 字数 259 浏览 4 评论 0原文

假设我有以下文本:

__This_is__ a __test__

使用两个下划线表示斜体。所以我希望 This_istest 为斜体。逻辑规定两个连续双下划线之间的任何文本都应为斜体,包括可能存在的任何其他数量的下划线。我得到了:

__([^_]+)__

第 1 组中“不是两个连续的下划线”相当于什么?谢谢。

Suppose I have the following text:

__This_is__ a __test__

Using two underscores for denoting italics. So I expect This_is and test to be italicized. The logic dictates that any text between two consecutive double underscores should be italicized, including any other number of underscores that may be there. I've got:

__([^_]+)__

What is the equivalent of "not two consecutive underscores" in group 1? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

前事休说 2024-10-22 16:13:48

一种选择是匹配两个下划线:

__

然后进行否定向前查看当前位置前面是否没有两个下划线:

__(?!__)

如果不是这种情况,则匹配任何字符:

__(?!__). 

并重复前一个或多次:

__((?!__).)+

最后匹配另外两个下划线:

__((?!__).)+__

这是最终的解决方案。

一个小演示:

<?php
$text = '__This_is__ a __test__';
preg_match_all('/__(?:(?!__).)+__/', $text, $matches);
print_r($matches);
?>

产生:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

)

Ideone 上所示。

编辑

请注意,我在演示中使用了非捕获组,否则输出将如下所示:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

    [1] => Array
        (
            [0] => s
            [1] => t
        )

)

((?!__).) 匹配的最后一个字符将被捕获在组中1.

有关组的更多信息,请参阅:http://www.regular-expressions.info/brackets.html

An option would be to match two underscores:

__

Then make a negative look ahead to see if theres no two underscores ahead of the current position:

__(?!__)

if that is not the case, match any character:

__(?!__). 

and repeat the previous one or more times:

__((?!__).)+

and finally match another two underscores:

__((?!__).)+__

which is the final solution.

A little demo:

<?php
$text = '__This_is__ a __test__';
preg_match_all('/__(?:(?!__).)+__/', $text, $matches);
print_r($matches);
?>

produces:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

)

as can be seen on Ideone.

EDIT

Note that I used a non-capturing group in my demo, otherwise the output would have looked like this:

Array
(
    [0] => Array
        (
            [0] => __This_is__
            [1] => __test__
        )

    [1] => Array
        (
            [0] => s
            [1] => t
        )

)

i.e. the last character matched by ((?!__).) would have been captured in group 1.

More about groups, see: http://www.regular-expressions.info/brackets.html

甜柠檬 2024-10-22 16:13:48
$text = '__This_is__ a __test__';
preg_match_all('/(__([\w]+)__)/', $text, $matches);
print_r($matches);

http://ideone.com/uHJCC

$text = '__This_is__ a __test__';
preg_match_all('/(__([\w]+)__)/', $text, $matches);
print_r($matches);

http://ideone.com/uHJCC

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文