gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:28:10 24 4
gpt4 key购买 nike

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

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

但是我在 C# 中使用 regx 时遇到问题并得到这个错误

Match Pattern Error: parsing "[([?)(blockquote)(?![\w-])([^]/](?:/(?!])[^]/])*?)(?:(/)]|](?:([^[]+(?:[(?!/\2])[^[]+)*+)[/\2])?)(]?)" - Nested quantifier +.

请指教。

生成php正则表达式

[blockquote] some text... [/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]]

最佳答案

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

因此,您需要将 [0-9]*+ 等结构替换为 (?>[0-9]*)

关于c# - 将 PHP 正则表达式转换为 c# RegEx 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21579131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com