Java正则表达式匹配错误,原因是$
我有以下正则表达式片段来解析ahref的URL,如下所示:Java正则表达式匹配错误,原因是$,java,regex,Java,Regex,我有以下正则表达式片段来解析ahref的URL,如下所示: (?<=href=)[^\"']+(?=(\"|'))?> 如何修复正则表达式以匹配这里的,试一试。替换这个 (?<=href=)(['"]?)\\$([^$>]+)\\$ 不要忘了避开你的反斜杠。如果我正确理解了你的问题,你试图用你的应用程序中的某个值替换$someIdentifier$形式的模式,你正在使用someIdentifier取消引用 似乎您希望使用模式\$([^\$]+)\$,找到字符串中的每个
(?<=href=)[^\"']+(?=(\"|'))?>
如何修复正则表达式以匹配这里的
,试一试。替换这个
(?<=href=)(['"]?)\\$([^$>]+)\\$
不要忘了避开你的反斜杠。如果我正确理解了你的问题,你试图用你的应用程序中的某个值替换$someIdentifier$
形式的模式,你正在使用someIdentifier取消引用
似乎您希望使用模式\$([^\$]+)\$
,找到字符串中的每个匹配项,获取第一组(1)的值,查找该值,然后用您查找的值替换该特定序列的所有匹配项
String someString = "some$string$withatoken";
Pattern tokenPattern = Pattern.compile("\\$([^\\$]+)\\$");
Matcher tokenMatcher = tokenPattern.matcher(someString);
// find not matches. matches will compare the entire string against the pattern
// basically enforcing ^{pattern}$.
while (tokenMatcher.find()) {
String tokenName = tokenMatch.group(1);
String tokenValue = tokenMap.get(tokenName); // lookup value of token name.
someString = someString.replaceAll("\\$" + tokenName + "\\$", tokenValue);
// resets the target of the matcher with the new value of someString.
tokenMatcher.reset(someString);
}
嗯。。我尝试了你的regexp,没有得到错误。但是,regexp并没有替换$click\u tracking\u url$
,而是替换整个文本,直到元素结束
如果需要动态替换占位符,请尝试以下操作:
Map<String, String> placeholders = new HashMap<String, String>();
// init your placeholders somehow...
placeholders.put("click_tracking_url", "something");
String fragment = "<a href=\"$click_tracking_url$&landing_url=google.com\"><img src=\"10.gif\" /></a>";
Matcher m = Pattern.compile("\\$(\\w+)\\$").matcher(fragment);
if (m.find()) {
String processedFragment = m.replaceAll(placeholders.get(m.group(1)));
System.out.println(processedFragment);
}
Map占位符=新HashMap();
//不知何故初始化你的占位符。。。
占位符。放置(“单击跟踪url”,“某物”);
字符串片段=”;
Matcher m=Pattern.compile(“\\$(\\w+\\\$”).Matcher(片段);
如果(m.find()){
String processedFragment=m.replaceAll(占位符.get(m.group(1));
System.out.println(processedFragment);
}
我认为量化环顾断言会导致未定义的行为。不过我很困惑。如果你想让它成为可选的,那么断言到底有什么意义。。。?(我说的是(?=…)?
)我不是在做选择$tracking_url$从文件中读取,并将替换为其他服务中的tracking_url$跟踪url$未用于任何正则表达式。老实说,我认为失败与$
无关。我认为问题在于您将量词?
应用于零宽度断言(?=…)
。另外,你确定剩下的正则表达式有意义吗?您正在查找一个非引号字符字符串,该字符串前面是href=
,后面是一个(可能是)结束引号字符,后面是一个从未打开过的引号字符。。。
(?<=href=)(['"]?)\\$([^$>]+)\\$
$1$url
String someString = "some$string$withatoken";
Pattern tokenPattern = Pattern.compile("\\$([^\\$]+)\\$");
Matcher tokenMatcher = tokenPattern.matcher(someString);
// find not matches. matches will compare the entire string against the pattern
// basically enforcing ^{pattern}$.
while (tokenMatcher.find()) {
String tokenName = tokenMatch.group(1);
String tokenValue = tokenMap.get(tokenName); // lookup value of token name.
someString = someString.replaceAll("\\$" + tokenName + "\\$", tokenValue);
// resets the target of the matcher with the new value of someString.
tokenMatcher.reset(someString);
}
Map<String, String> placeholders = new HashMap<String, String>();
// init your placeholders somehow...
placeholders.put("click_tracking_url", "something");
String fragment = "<a href=\"$click_tracking_url$&landing_url=google.com\"><img src=\"10.gif\" /></a>";
Matcher m = Pattern.compile("\\$(\\w+)\\$").matcher(fragment);
if (m.find()) {
String processedFragment = m.replaceAll(placeholders.get(m.group(1)));
System.out.println(processedFragment);
}