如何匹配以下順序:相似但不相同的字符串序列
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