在scala中,我需要替换一个字符串 %23
与 #
,如下所示:
从 https://bucket_name.s3.amazonaws.com/scripts/%23%23%23ENVIRONMENT_NAME%23%23%23//template__windows_%23%23%23ENVIRONMENT_NAME%23%23%23.zip?X-Amz-Security-Token=FQoGZXIvYXdzEOghsfgdghgkjkjjklj
至 https://bucket_name.s3.amazonaws.com/scripts/###ENVIRONMENT_NAME###//template__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)
非常感谢您的帮助。谢谢。
1条答案
按热度按时间m4pnthwp1#
也许你可以用
String.replaceAll
?