我需要修改PHP降价的正则表达式的帮助 [英] I need help modifying a regular expression for PHP markdown

查看:78
本文介绍了我需要修改PHP降价的正则表达式的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改 PHP Markdown (标记语言的PHP解析器,它是尝试在Stack Overflow上使用)尝试实现Jeff在此博客中描述的第1点,第2点和第3点发布.我已经轻松完成了最后两个,但是事实证明这很困难:

I'm modifying PHP Markdown (a PHP parser of the markup language which is used here on Stack Overflow) trying to implement points 1, 2 and 3 described by Jeff in this blog post. I've easily done the last two, but this one is proving very difficult:

  1. 删除了对词内支持 重点like_this_example
  1. Removed support for intra-word emphasis like_this_example

实际上,在常规"降价实现中,like_this_example的呈现方式类似于 this 的示例.这是非常不可取的.我只希望_example_成为示例.

In fact, in the "normal" markdown implementation like_this_example would be rendered as likethisexample. This is very undesirable; I want only _example_ to become example.

我查看了源代码,发现用于强调的正则表达式:

I looked in the source code and found the regex used to do the emphasis:

var $em_relist = array(
    ''  => '(?:(?<!\*)\*(?!\*)|(?<!_)_(?!_))(?=\S|$)(?![.,:;]\s)',
    '*' => '(?<=\S|^)(?<!\*)\*(?!\*)',
    '_' => '(?<=\S|^)(?<!_)_(?!_)',
    );
var $strong_relist = array(
    ''   => '(?:(?<!\*)\*\*(?!\*)|(?<!_)__(?!_))(?=\S|$)(?![.,:;]\s)',
    '**' => '(?<=\S|^)(?<!\*)\*\*(?!\*)',
    '__' => '(?<=\S|^)(?<!_)__(?!_)',
    );
var $em_strong_relist = array(
    ''    => '(?:(?<!\*)\*\*\*(?!\*)|(?<!_)___(?!_))(?=\S|$)(?![.,:;]\s)',
    '***' => '(?<=\S|^)(?<!\*)\*\*\*(?!\*)',
    '___' => '(?<=\S|^)(?<!_)___(?!_)',
    );

我尝试在 Regex Buddy 中打开它,但还不够,花了一半时间一个小时的工作,我仍然不知道从哪里开始.有什么建议吗?

I tried to open it in Regex Buddy but it wasn't enough, and after spending half an hour working on it I still don't know where to start. Any suggestions?

有些人在面对 问题,以为我知道,我会用 正则表达式."现在,他们有了 两个问题.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

推荐答案

我只能通过以下方式捕获单个_enclosed_单词:

I was able to grab only individual _enclosed_ words via:

$input = 'test of _this_ vs stuff_like_this...and here is _anothermatch_ and_another_fake_string';
$pattern = '#(?<=\s|^)(?<!_)(_[^_]*_)(?!_)#is';
preg_match_all($pattern, $input, $matches);
print_r($matches);

我不确定上述代码到底适合什么.您可能需要将其与下面的其他模式配对,以说明两种和三种匹配情况:

I'm not sure how exactly that would fit into the above code though. You would probably need to pair it with the other patterns below to account for the two and three match situations:

$pattern = '#(?<=\s|^)(?<!_)(__[^_]*__)(?!_)#is';
$pattern = '#(?<=\s|^)(?<!_)(___[^_]*___)(?!_)#is';

这篇关于我需要修改PHP降价的正则表达式的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆