在scala中搜索和替换字符串中的特殊字符

beq87vna  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(446)

在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=FQoGZXIvYXdzEOghsfgdghgkjkjjkljhttps://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)

非常感谢您的帮助。谢谢。

m4pnthwp

m4pnthwp1#

也许你可以用 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", "#")

相关问题