[英]How to escape special characters in a regex pattern in java?
我有一個字符串:
Patient:
${ss.patient.howard.firstName} ${ss.patient.howard.lastName}
Gender: ${ss.patient.howard.sex}
Birthdate: ${ss.patient.howard.dob}
${ss.patient.howard.addressLine1}
Phone: (801)546-4765
並且我正在嘗試將${..}
子字符串替換${..}
其他字符串,使其看起來像:
Patient:
firstName lastName
Gender:sex
Birthdate: dob
addressLine1
Phone: (801)546-4765
您可以將此正則表達式與捕獲組一起使用:
String myString = "Patient:\n${ss.patient.howard.firstName} ${ss.patient.howard.lastName}\nGender: ${ss.patient.howard.sex}\nBirthdate: ${ss.patient.howard.dob}\n${ss.patient.howard.addressLine1}\nPhone: (801)546-4765";
myString = myString.replaceAll("\\$\\{[^}]+?\\.([^.}]+)}", "$1");
System.err.println(myString);
([^.}]+)
是}
之前和最后一個DOT之后的捕獲組。
輸出:
Patient:
firstName lastName
Gender: sex
Birthdate: dob
addressLine1
Phone: (801)546-4765
你可以嘗試搜索${string.string.string
OR }
並將其替換empty
字符串或nothing
這樣的。
正則表達式: \\${[az]+\\.[az]+\\.[az]+\\.|}
替換操作:替換為空或空字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.