将PHP正则表达式转换为c#RegEx模式

时间:2014-02-05 13:46:18

标签: c# regex

我的问题很小。我必须将正则表达式从php转换为C#

\[(\[?)(blockquote)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)

但是在c#中使用regx时出现问题并出现此错误

  

匹配模式错误:解析“[([?)(blockquote)(?![\ w - ])([^] /] (?:/(?!])[^] /] < / em>的)*)(:(/)] |](:???([??!^ [] +(:[(/ \ 2])[^ [] +) * +)[/ \ 2])?)(]?)“ - 嵌套量词+。

请建议。

生成php regex

  

[blockquote]一些文字...... [/ blockquote]

来自

的wordpress中的

function get_shortcode_regex() {
global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );

// WARNING! Do not change this regex without changing do_shortcode_tag() and strip_shortcode_tag()
// Also, see shortcode_unautop() and shortcode.js.
return
      '\\['                              // Opening bracket
    . '(\\[?)'                           // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
    . "($tagregexp)"                     // 2: Shortcode name
    . '(?![\\w-])'                       // Not followed by word character or hyphen
    . '('                                // 3: Unroll the loop: Inside the opening shortcode tag
    .     '[^\\]\\/]*'                   // Not a closing bracket or forward slash
    .     '(?:'
    .         '\\/(?!\\])'               // A forward slash not followed by a closing bracket
    .         '[^\\]\\/]*'               // Not a closing bracket or forward slash
    .     ')*?'
    . ')'
    . '(?:'
    .     '(\\/)'                        // 4: Self closing tag ...
    .     '\\]'                          // ... and closing bracket
    . '|'
    .     '\\]'                          // Closing bracket
    .     '(?:'
    .         '('                        // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
    .             '[^\\[]*+'             // Not an opening bracket
    .             '(?:'
    .                 '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
    .                 '[^\\[]*+'         // Not an opening bracket
    .             ')*+'
    .         ')'
    .         '\\[\\/\\2\\]'             // Closing shortcode tag
    .     ')?'
    . ')'
    . '(\\]?)';                          // 6: Optional second closing brocket for escaping shortcodes: [[tag]]

}

1 个答案:

答案 0 :(得分:1)

嗯,根据this回答,.NET不支持有意义的量词。

因此,您需要使用类似[0-9]*+

的smth替换(?>[0-9]*)等结构