[英]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.