如何匹配以下顺序:相似但不相同的字符串序列
You wound DUMMY TARGET for 100 points of damage
但不是:
You wound DUMMY TARGET with SKILL for 100 points of damage
正则表达式:
^You wound ([\\w\\s]+)(?!with) for (\\d+) points of damage
正则表达式以上匹配两个字符串,而我希望只匹配第一个。有什么办法可以做到这一点?
样品Java代码:
import java.util.regex.*;
public class Dummy {
static Pattern pattern = Pattern.compile("^You wound ([\\w\\s]+)(?!with) for (\\d+) points of damage");
static Matcher matcher = pattern.matcher("");
static String FIRST_SEQUENCE = "You wound DUMMY TARGET for 100 points of damage";
static String SECOND_SEQUENCE = "You wound DUMMY TARGET with SKILL for 100 points of damage";
public static void main(String...args) {
if (matcher.reset(FIRST_SEQUENCE).matches())
System.out.println("First match. Ok!");
if (matcher.reset(SECOND_SEQUENCE).matches())
System.out.println("Second match. Wrong!");
}
}
”with SKILL“总是固定的? – YOU 2009-12-18 14:51:32
单词“with”始终是固定的。技能可以是一个或多个间隔的单词。 这些是真正的序列示例: “你伤害了Ongbúrz狂战士68点古代矮人造成的伤害。” “你用Valar的Anthem伤害了Ongbúrz狂战士,造成了215点古代矮人造成的伤害。” – 2009-12-18 17:34:03