首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >处理成对转义括号的Regex

处理成对转义括号的Regex
EN

Stack Overflow用户
提问于 2018-06-03 16:01:51
回答 1查看 69关注 0票数 1

现在的情况是,我需要匹配一个字符串

1)以“$”开头,以“$”结尾;

( 2)中间不存在“$”,只对括号“{”、“}”进行转义;

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

4)字符串可以有多行。

例如,

有效:

1,2,3,4,5,6美元 ${$ 1,2,3} $ //中间的'$‘由括号转义 $ {1,2},3 }$ $ {1,2 }\r\n ,3 }r}$ //多行

无效:

$ 1,2$ $ {1,2 $ //如果有任何括号,它们必须匹配 $ {1,2,$,3},4 $ //中间的'$‘不是用一对括号转义的。 ${$ 1,2 }$

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-03 19:23:59

由于您没有指定您使用的是哪种语言,所以我已经使用java实现了一个工作原型。

正如tim在他的评论中提到的,Regex不是嵌套模式的合适工具。为了解决问题,您可以在一次传递中使用以下方法:

  • 对字符串的大小以及它是否以$开头/结尾进行一些基本检查
  • 逐个字符读取字符串字符(忽略极值),并检查每个$前面的字符是否为{
  • 使用堆栈推送和弹出{,如果堆栈是空的,并且您想弹出一个{ --这意味着在处理结束时,字符串的格式不是很好,您应该有一个空堆栈。

样本:

代码语言:javascript
运行
AI代码解释
复制
    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;

    }

输入:

代码语言:javascript
运行
AI代码解释
复制
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 {} $"));

输出:

代码语言:javascript
运行
AI代码解释
复制
$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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50672038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文