我需要将 https://9S&Re
替换为 https://9S#Re
,这样我就可以避免拆分逻辑。我的目标是它的键为“user_image”,值为“https://9S&Re”
。但是当我将 split 与 '&' 一起使用时,它会给出错误的结果。因此,我正在尝试使用正则表达式模式,该模式将检查 &*& 之间是否存在不等于,则意味着查询值之一中有一个&符号。非常感谢任何帮助。
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
public class OAuthTest {
public static void main(String[] args) {
try {
//HashMap paramHashMap=new HashMap();
List<NameValuePair> args2= URLEncodedUtils.parse("tool=canvas&tool_guid=8314&user_image=https://9S&Re&launch_presentation_document_target=iframe", Charset.forName("UTF-8"));
for (NameValuePair arg:args2)
System.out.println(arg.getName()+"="+arg.getValue().replaceAll("#","&"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
在 for 循环中,您是否尝试将 & 替换为 # ?你的论点已经改变了。
关于java - 用特殊字符替换 & 符号的正则表达式模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20888571/