[英]How can I manipulate part of a string in java using regular expressions
我有一個看起來像這樣的字符串:
String pathTokenString = "CMS/{brandPath}/Shows/{showPath}";
我要刪除顯示部分以及隨后的所有內容。 我還想用獲得的令牌替換“ {brandPath}”。
這是我的方法。 但是,我的字符串根本沒有更新:
//remove the '/Shows/{showPath}'
pathTokenString = pathTokenString.replace("/Shows$", "");
//replace passed in brandPath in the tokenString
String answer = pathTokenString.replace("{(.*?)}", brandPath);
我的正則表達式有問題嗎?
當您要將正則表達式字符串作為要替換的模式時,應使用replaceAll
方法而不是replace
。 另外,您的正則表達式模式也應更新:
pathTokenString = pathTokenString.replaceAll("/Shows.*$", "");
// The curly braces need to be escaped because they denote quantifiers
String answer = pathTokenString.replaceAll("\\{(.*?)\\}", brandPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.