请我需要一些人来帮助我解决这个问题,我想使用正则表达式将 Dr、Rd 和 St 等字符替换为 Drive Road 和 Street,请问该怎么做?我已经尝试了很多方法,但我的结果是不正确的。例如,如果用户为变量 vaddress 提供此地址:13, Clinton str off allen rd queens dr。结果应该是 13 号,Clinton Street off allen Road Queens Drive。
这是我的代码示例:
public void standardizeAddress(String vaddress){
address = vaddress;//address is an instance variable
String str1,str2;
//Replace all instances of 'St,Rd,Dr' with 'Street,Road and Drive' Using
String regex = "\\sSt\\s";
Pattern pat = Pattern.compile(regex);
Matcher match = pat.matcher(address);
while(match.find())
address = match.replaceAll(" Street ");
System.out.println(address);
str1 = address;
regex = "\\sRd\\s";
pat = Pattern.compile(regex);
match = pat.matcher(str1);
while(match.find())
str1 = match.replaceAll(" Road ");
System.out.println(str1);
str2 = str1;
regex = "\\sDr\\s";
pat = Pattern.compile(regex);
match = pat.matcher(str2);
while(match.find())
str2 = match.replaceAll(" Drive ");
System.out.println(str2);
}
先感谢您