在scala中,我有一个字符串,需要在其中用%23
替换#
,如下所示:
来自https://bucket_name.s3.amazonaws.com/scripts/%23%23%23ENVIRONMENT_NAME%23%23%23/abc/template_abc_windows_%23%23%23ENVIRONMENT_NAME%23%23%23.zip?X-Amz-Security-Token=FQoGZXIvYXdzEOghsfgdghgkjkjjklj
到https://bucket_name.s3.amazonaws.com/scripts/###ENVIRONMENT_NAME###/abc/template_abc_windows_###ENVIRONMENT_NAME###.zip?X-Amz-Security-Token=FQoGZXIvYXdzEOghsfgdghgkjkjjklj
我在正则表达式和逻辑下面使用了替换,但出现错误:
java.lang.IllegalStateException: No match found
代码:
val originalURL = "https://bucket_name.s3.amazonaws.com/scripts/%23%23%23ENVIRONMENT_NAME%23%23%23/abc/template_abc_windows_%23%23%23ENVIRONMENT_NAME%23%23%23.zip?X-Amz-Security-Token=FQoGZXIvYXdzEOghsfgdghgkjkjjklj"
val pattern = Pattern.compile("(https://bucket_name.s3.amazonaws.com/scripts/)((%23){3})([a-zA-Z]+_[a-zA-Z]+)((%23){3})(/abc/template_abc_windows_)((%23){3})([a-zA-Z]+_[a-zA-Z]+)((%23){3})(..*)")
val matcher = pattern.matcher(originalURL)
val replacedURL = matcher.group(1)+"###"+ matcher.group(4)+"###"+ matcher.group(7)+"###"+ matcher.group(10)+"###"+matcher.group(13)
println("*******replacedURL******* => "+ replacedURL)
非常感谢您的帮助。谢谢。
答案 0 :(得分:1)
也许您可以只使用String.replaceAll
?
val url = "https://bucket_name.s3.amazonaws.com/scripts/%23%23%23ENVIRONMENT_NAME%23%23%23/abc/template_abc_windows_%23%23%23ENVIRONMENT_NAME%23%23%23.zip?X-Amz-Security-Token=FQoGZXIvYXdzEOghsfgdghgkjkjjklj"
url.replaceAll("%23", "#")