我正在尝试这样做:
我有这种文本(即文件):
[[dadasd sadasd sdsd ad asddd]] [[dasdsd]] dsdsd [[dsdas]] … [[dd ssas dd]]
我只想要双方括号之间的句子.我怎么能用java解决这个问题?
//This one is not working:
String patternStr = "(.*)\\[\\[(.*)\\]\\](.*)";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher("");
// Set the input
matcher.reset("[[sdasd]] ddd [[ddssssssssssss]] vvvddd [[dd]] asdasda [[asdsa]] ");
...
提前致谢
解决方法:
你想要以下正则表达式:
\[\[(.+?)\]\]
然后转换为Java字符串:
\\[\\[(.+?)\\]\\]
PowerShell测试:
PS Home:\> [regex]::Matches("[[sdasd]] ddd [[ddssssssssssss]] vvvddd [[dd]] asdasda [[asdsa]] ", '\[\[(.+?)\]\]') | ft -auto
Groups Success Captures Index Length Value
------ ------- -------- ----- ------ -----
{[[sdasd]], sdasd} True {[[sdasd]]} 0 9 [[sdasd]]
{[[ddssssssssssss]], ddssssssssssss} True {[[ddssssssssssss]]} 14 18 [[ddssssssssssss]]
{[[dd]], dd} True {[[dd]]} 40 6 [[dd]]
{[[asdsa]], asdsa} True {[[asdsa]]} 55 9 [[asdsa]]
标签:java,regex
来源: https://codeday.me/bug/20190721/1495630.html