我遇到了这个表达式,想弄清楚正则表达式会匹配什么?
new Text(Arrays.toString(parts).replaceAll("^\\[|\\]$", "")
我用了Xeger生成一些字符串,但它可能没有显示正确的字符串,因为它一直只生成 2 个字符串 ^[ 和 ]$
最佳答案
您的正则表达式所做的是匹配起始 [
和结束 ]
来自 regex101 的解释:
1st Alternative: ^\[
^ assert position at start of a line
\[ matches the character [ literally
2nd Alternative: \]$
\] matches the character ] literally
$ assert position at end of a line
所以,这段代码:
"[sdfsad sdfsd]asdfas]".replaceAll("^\\[|\\]$", "")
将删除开始和结束的 [
]
,结果字符串将是:
sdfsad sdfsd]asdfas
关于java - 这个正则表达式匹配什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839344/