我希望能够匹配以下内容:
[$first-last.something.random.more:junk]
示例:
[$first-last.something.random.more:junk] [correct]
[$first-last.some.more:junk, $first-last.more:values] [correct]
[$first-last.some.more:junk, $first-last.more:values]
[$first-last.single.correct:type] [correct]
[$blah-garbage.some.more:junk, $first-last.more:values] [incorrect]
[$first-last.some.more, $first-last.more:values] [incorrect]
到目前为止,我所拥有的是:
^\s*(\[\$[first\-last|cat\-dog]+(\.[a-zA-Z0-9---_]+)+\s*:\s*+[a-zA-Z0-9---_]+(?:,\s*\$[first\-last|cat\-dog]+(\.[a-zA-Z0-9---_]+)+\s*:\s*+[a-zA-Z0-9---_]+)\]\s*)\s*$
这不适用于这种情况:
[$first-last.some.more:junk, $first-last.more:values]
[$first-last.single.correct:type]
它也无法在Java中工作。没有错误,它只是说找不到匹配,即使它有效并且可以在以下网站上找到匹配项:https://regex101.com/
答案 0 :(得分:2)
尝试以下正则表达式:
\[(?:\$first-last|cat-dog)(?:\.[\w-]+)+[\w-]+:[\w-]+(?:,\s*(?:\$first-last|cat-dog)(?:\.[\w-]+)+[\w-]+:[\w-]+)*\]
请参阅here