在Java中使用引号匹配模式

时间:2016-01-12 20:09:52

标签: java regex string compare matching

我想知道如何使用模式来检查正则表达式是否与字符串匹配?我遇到的问题是,我希望引号中的文字像一个单词一样,就像第二种情况一样。

字符串:

message "the id of %type of the clicked block% is %id of the clicked block%."

语法:

message .*

应该回归:

true

另一个案例: 字符串:

send "You are not allowed to use this command." 

语法:

send .* to .*

应该回归:

False

1 个答案:

答案 0 :(得分:0)

正如我在上面的评论中所指出的那样,希望这可以回答你的问题,但你可以简单地在引号上终止并匹配引号之间的任何内容,如下所示:

给出你的例子:

message "the id of %type of the clicked block% is %id of the clicked block%."

send "You are not allowed to use this command." 

与这两者相匹配的正则表达式包括使用\".*\"匹配引号之间的所有内容(由\转义)。

\w+ \".*\"

因此,在这种情况下,使用send \".*\" to将是错误的。

介绍匹配组:

send (\".*\")