如何关闭所有未关闭的左方括号标签?

发布于 2024-12-04 18:00:27 字数 1173 浏览 4 评论 0原文

好的,所以我有一个消息,可以使用正则表达式来判断里面是否有方括号,如果有,判断它们是否不是封闭的。

例如,左方括号是这样的: [code]

结束方括号是这样的: [/code]

但是,除了 BBC 代码之外,还有更多方法可以位于方括号内。

我希望能够做的是使用包含整个消息的变量,并以某种方式确定方括号内是否有任何没有结束标记的单词,该标记表示为: [/ 这个词,然后比 ] 开始标签当然以 [ 这个词开始,然后以 ] 结束

所以,如果我在变量中有这样的东西:

好的,这是整个 script.php文件中包含所有最近的模块代码。因此,我们从 Main 函数开始,从函数参数中检索 $params...

[code]function module_recent($params)
{
   global $context, $txt;

   // Grab the params, if they exist.
   if (is_array($params))
   {

它会知道 [code] 没有关闭,并将其添加到末尾 [/code]

而且,如果我有这样的东西:

[table]
[tr][td]Hello World[/td][/tr]
[tr][td]This is not closed...

它应该知道 [table][tr][td]未关闭,应按以下顺序在末尾添加结束标签:

[/td] 然后是 [/tr] 最后是 [/table]

但还有其他标签,例如 [list] [li][/li][/list]

如果我可以填充数组中方括号内的所有标签,然后调用一个函数来检查它是否同时具有开始和结束,那就太好了结束标签,这样就不会影响人们放置的非 BBC 代码标签进入消息只是原因。

任何人都可以帮我注册 Reg. 吗?前任要做什么?至少如果有人能帮助我开始这件事那就太好了。

谢谢大家:)

Ok, so I have a message, can a regular expression be used to determine if there are square brackets within it, and if so, determine if they aren't closed.

For example, and opening square bracket is like this: [code]

Closing is like this: [/code]

But there are way more than just the code bbc codes that can be within square brackets.

What I'd like to be able to do, is use a variable that contains the entire message, and somehow determine if there are any words within square brackets that do not have a closing tag, which is denoted by: [/ the word, and than ]
Opening tags ofcourse start with [ the word, and than end with ]

So, if I have something like this within a variable:

Ok, so here is the overall script.php file with ALL of the Recent Module code in it. So we start with the Main function for retrieving the $params from the functions parameter...

[code]function module_recent($params)
{
   global $context, $txt;

   // Grab the params, if they exist.
   if (is_array($params))
   {

It would know that [code] was not closed off and add it at the end [/code]

But also, if I have something like this:

[table]
[tr][td]Hello World[/td][/tr]
[tr][td]This is not closed...

It should know that [table] and [tr] and [td] is not closed and it should add the closing tags into it at the end in this order:

[/td] and than [/tr] and finally [/table]

But there are also other tags like [list][li][/li][/list]

Would be great if I could populate all of the tags that can be within square brackets within an array and than call upon a function that would check if it has both opening and closing tags, that way it wouldn't effect non-bbc code tags that people put into messages just cause.

Can anyone give me a hand on a Reg. Ex to do this with? Atleast if someone can help me get started on this that would be excellent.

Thanks guys :)

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

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

发布评论

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

评论(1

多孤肩上扛 2024-12-11 18:00:27

我要做的是编写一个扫描器和一个解析器。平衡括号问题是语言理论中的经典问题。

正则表达式可用于模式匹配和标记提取。你的问题是一个语法问题,你需要一个解析器来解决这样的问题。

在这种情况下不需要复杂的解析器。一个栈就够了。请参阅下面的高级算法。

enum TokenType{
    StartTag,
    EndTag,
    Text
}

struct Token {
    string Value;
    TokenType TokenType;
}

Token GetNextToken() {
    // returns the next token in the input string or null if end of the string.
}

bool MatchingTags(Token startTag, Token endTag)){
    // check if startTag and endTag match
}

bool CheckTags(){
    Stack stack = new Stack();
    while( (Token t = GetNextToken()) != null )
    {
        switch(t.TokenType){
            case TokenType.StartTag:
                stack.push(t);
            break;
            case TokenType.EndTag:
                Token lastPushed = stack.pop();
                if( ! MatchingTags(lastPushed, t)){
                    return false
                }
            break;
        }
    }
    if (! stack.IsEmpty()){
        return false
    }else{
        return true;
    }
}

注意:此算法还会检查正确的嵌套:[A][B][/A][/B] 无效
注意:这只是一个代码示例,旨在为您提供一个想法。请根据您的编程语言/框架对其进行完善和调整。

What I would do is to write a scanner and a parser. The problem of balanced parenthesis is a classic in language theory.

Regular expressions can be used for pattern matching and token extraction. Your problem is a grammatical problem, and you need a parser to solve such problem.

No need for a sophisticated parser in this case. A stack is enough. See the high level algorithm below.

enum TokenType{
    StartTag,
    EndTag,
    Text
}

struct Token {
    string Value;
    TokenType TokenType;
}

Token GetNextToken() {
    // returns the next token in the input string or null if end of the string.
}

bool MatchingTags(Token startTag, Token endTag)){
    // check if startTag and endTag match
}

bool CheckTags(){
    Stack stack = new Stack();
    while( (Token t = GetNextToken()) != null )
    {
        switch(t.TokenType){
            case TokenType.StartTag:
                stack.push(t);
            break;
            case TokenType.EndTag:
                Token lastPushed = stack.pop();
                if( ! MatchingTags(lastPushed, t)){
                    return false
                }
            break;
        }
    }
    if (! stack.IsEmpty()){
        return false
    }else{
        return true;
    }
}

Note: This algorithm also checks proper nesting: [A][B][/A][/B] is not valid
Note: This is just a code sample to give you an idea. Please refine and adjust it to your programming language/framework.

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