处理配对转义括号的正则表达式

时间:2018-06-04 00:01:51

标签: regex

这是情况,我需要匹配一个

的字符串

1)以' $'开头。并以' $'结尾;

2)没有' $'介于两者之间,除了被括号括起来之外' {','}'

3)可以有嵌套括号,每个左括号必须有一个匹配的右括号。

4)字符串可以有多行。

例如,

有效:

  

$ $ 1,2,3,4,5,6

     

$ {$ 1,2,3} $ //中间' $'被括号

转义      

$ {{$ {1,2},3}} $

     

$ {{$ {1,2} \ r \ n,3} \ r \ n} $ //多行

无效:

  

$ 1,2 $ $

     

$ {1,2 $ //如果有任何括号,则必须匹配

     

$ {1,2,$,3},4 $ //中间' $'不会被一对括号转义

     

$ {$} {1,2 {} $

这看起来相当复杂。我有一个初步的解决方案似乎适用于1),2)和4),但它无法检查是否每个左括号' {'有一个正确的右括号'}'。

\$(([^\$]*(\{[\s\S]+\})+[^\$]*)|([^\$]+))\$

1 个答案:

答案 0 :(得分:1)

由于您尚未指定使用的语言,因此我使用java实现了工作原型。

正如tim在他的评论中提到的,当它出现嵌套模式时,正则表达式不是正确的工具。为了解决您的问题,您可以一次性使用以下方法:

  • 对字符串的大小进行一些基本检查,并以$
  • 开头/结尾
  • 逐个字符地阅读字符串(忽略四肢)并检查每个$前面的字符是{
  • 使用堆栈推送并弹出{}如果堆栈为空并且您想弹出{这意味着该字符串格式不正确处理结束你应该有一个空堆栈

<强>示例:

    public static boolean stringBracketsValidator(String input)
    {
        //enforce the constraint that the string is not null, its length is at least 2
        if(input == null || input.length() < 2)
            return false;
        char[] charArray = input.toCharArray();
        //starts and ends with $ constraint
        if(charArray[0]!= '$' || charArray[charArray.length-1] != '$')
            return false;
        //stack  to validate the nesting and the $ proper escape
        Stack<Character> bracketNestingValidator = new Stack<Character>();
        //we are not processing the extrimities of the char array
        for(int i = 1; i < charArray.length-1; i++)
        {
            /*while reading the input string if we reach a dollar character
             * we check that it is preceded by an opening bracket
             */
            if(charArray[i] == '$')
                if(charArray[i-1] != '{')
                    return false;
            //when we reach an opening bracket we put it on the stack 
            if(charArray[i] == '{')
                bracketNestingValidator.push('{');
            //when we reach a closing bracket we check that the stack is not empty
            if(charArray[i] == '}')
                if(bracketNestingValidator.size() == 0 )
                    return false;
                else
                    bracketNestingValidator.pop();//we pop the opening bracket we have pushed on the stack
        }

        //check that the stack is empty at the end of the run
        if(bracketNestingValidator.size() == 0)
            return true;
        return false;

    }

INPUT:

System.out.println("$1,2,3,4,5,6$ string: "+stringBracketsValidator("$1,2,3,4,5,6$"));
System.out.println("null string: "+stringBracketsValidator(null));
System.out.println("empty string: "+stringBracketsValidator(""));
System.out.println("1-sized string: "+stringBracketsValidator("1"));
System.out.println("2-sized string: "+stringBracketsValidator("11"));
System.out.println("$$ string: "+stringBracketsValidator("$$"));
System.out.println("${$ string: "+stringBracketsValidator("${$"));
System.out.println("${}$ string: "+stringBracketsValidator("${}$"));
System.out.println("${$}$ string: "+stringBracketsValidator("${$}$"));
System.out.println("${$$}$ string: "+stringBracketsValidator("${$$}$"));
System.out.println("${${$}}$ string: "+stringBracketsValidator("${${$}}$"));
System.out.println("${$1,2,3}$ string: "+stringBracketsValidator("${$1,2,3}$"));
System.out.println("${{${1,2}\r\n,3}\r\n }$ string: "+stringBracketsValidator("${{${1,2}\r\n,3}\r\n }$"));

System.out.println("$ 1,2$ $ string: "+stringBracketsValidator("$ 1,2$ $"));
System.out.println("$ {1,2 $ string: "+stringBracketsValidator("$ {1,2 $"));
System.out.println("$ {1,2 }}$ string: "+stringBracketsValidator("$ {1,2 }}$"));
System.out.println("$}1,2$ string: "+stringBracketsValidator("$}1,2$"));
System.out.println("$ {1,2,$,3},4 $ string: "+stringBracketsValidator("$ {1,2,$,3},4 $"));  
System.out.println("$ {$} { 1,2 {} $ string: "+stringBracketsValidator("$ {$} { 1,2 {} $"));

<强>输出:

$1,2,3,4,5,6$ string: true
null string: false
empty string: false
1-sized string: false
2-sized string: false
$$ string: true
${$ string: false
${}$ string: true
${$}$ string: true
${$$}$ string: false
${${$}}$ string: true
${$1,2,3}$ string: true
${{${1,2}
,3}
 }$ string: true
$ 1,2$ $ string: false
$ {1,2 $ string: false
$ {1,2 }}$ string: false
$}1,2$ string: false
$ {1,2,$,3},4 $ string: false
$ {$} { 1,2 {} $ string: false