vertx服务中java正则表达式的替换
我想在vertx服务中进行一些正则表达式替换,从数据库中查询要替换的文本。在我的例子中,我想用这个url的较短版本替换文本中的所有url
类似这样的事情:(我知道这不管用)
public String replaceUrls(String text){
String urlValidationRegex = "((https?|ftp)://)?(www\\d?|[a-zA-Z0- 9]+)?.[a-zA-Z0-9-]+(\\:|.)([a-zA-Z0-9.]+|(\\d+)?)([/?:].*)?";
Pattern p = Pattern.compile(urlValidationRegex);
Matcher m = p.matcher(text);
StringBuffer sb = new StringBuffer();
while(m.find()){
String found = m.group(0);
//make async database call
String shortUrl = urlShortenerService.rxGetShortUrl(found);
m.appendReplacement(sb, shortUrl);
}
m.appendTail(sb);
return sb.toString();
}
我不知道如何将reactivex数据库调用与regex字符串替换结合起来
有人能给我指出正确的方向吗
谢谢!
# 1 楼答案
我通过在其周围实现一个包装器对象来解决这个问题:
包装器类(称为Text):
有谁有更好的解决方案吗