现在的情况是,我需要匹配一个字符串
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]+\})+[^\$]*)|([^\$]+))\$
发布于 2018-06-03 19:23:59
由于您没有指定您使用的是哪种语言,所以我已经使用java
实现了一个工作原型。
正如tim在他的评论中提到的,Regex不是嵌套模式的合适工具。为了解决问题,您可以在一次传递中使用以下方法:
$
开头/结尾进行一些基本检查$
前面的字符是否为{
{
,如果堆栈是空的,并且您想弹出一个{
--这意味着在处理结束时,字符串的格式不是很好,您应该有一个空堆栈。样本:
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;
}
输入:
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
https://stackoverflow.com/questions/50672038
复制相似问题