开发者

How to close all opening square bracket tags that aren't closed?

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 开发者_Go百科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 :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜