提供了以下字符串:

@NAMEONE=any@character@OTHERNAME=any @ character@THIRDNAME=even new lines
are possible


如何匹配@NAMEONE=any@character等全名/值对?

我坚持使用此正则表达式(@(?:NAMEONE|OTHERNAME|THIRDNAME)=.+?)+,因为它仅匹配@NAMEONE=a@OTHERNAME=a等。使用Java。

最佳答案

仅当@的前一个字符和后一个字符为非单词字符时,此字符才能匹配任何字符,但不能匹配@@

"@(?:NAMEONE|OTHERNAME|THIRDNAME)=(?:\\B@\\B|[^@])*"


DEMO

要么

"(?s)@(?:NAMEONE|OTHERNAME|THIRDNAME)=.*?(?=@(?:NAMEONE|OTHERNAME|THIRDNAME)=|$)"


DEMO

07-27 15:44