gpt4 book ai didi

java - 如何解决未封闭字符类错误

转载 作者:行者123 更新时间:2023-12-03 07:51:48 25 4
gpt4 key购买 nike

这是我的代码:

string.replaceAll("(?i)["+key+"]\\S*", "[blue]$0[blue]")
它抛出未封闭的角色类

最佳答案

使用组,不要忘记逃避其他选择:

import java.util.*;
import java.util.stream.*;
import java.util.regex.*;

class app
{
public static void main (String[] args) throws java.lang.Exception
{
List<String> key = Arrays.asList("as","if","when","and","then");
List<String> alternatives = key.stream()
.map(Pattern::quote)
.collect(Collectors.toList());
String string = "AS?! When then";
System.out.println( string.replaceAll("(?i)(?:"+String.join("|", alternatives)+")\\S*", "[blue]$0[blue]") );
}
}

[**Java code demo**][1]
结果: [blue]AS?![blue] [blue]When[blue] [blue]then[blue]

关于java - 如何解决未封闭字符类错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64225351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com