我想使用Java中的正則表達式來提取特定的字符串。我目前有這種模式:正則表達式匹配Java中字符串的開頭和結尾
pattern = "^\\a.+\\sed$\n";
假設匹配以「a」開頭並以「sed」結尾的字符串。這不起作用。我錯過了什麼 ?
刪除模式末尾的\ n行,並將其替換爲「$」: 仍然無法匹配。正則表達式在我看來是合法的。
我想提取的是來自temp字符串的「a sed」。
String temp = "afsgdhgd gfgshfdgadh a sed afdsgdhgdsfgdfagdfhh";
pattern = "(?s)^a.*sed$";
pr = Pattern.compile(pattern);
math = pr.matcher(temp);
試試這個'^a。* sed $' – nafas
是您尋找的「sed」嗎? – nafas