Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的ReplaceAll_Java_Regex_Replaceall - Fatal编程技术网

Java中的ReplaceAll

Java中的ReplaceAll,java,regex,replaceall,Java,Regex,Replaceall,我习惯于将单词大写 因为我不能定义哪些单词应该大写,所以我必须在大写函数之后进行另一个实现,以将一些单词小写 应该小写的单词是:[“da,de,di,do,du,das,des,dis,dos,dus”] 所以,我现在的code是: public static String capitalize(String word) { String newWord = WordUtils.capitalizeFully(word); newWord = newWord.replaceAll("

我习惯于将单词大写

因为我不能定义哪些单词应该大写,所以我必须在大写函数之后进行另一个实现,以将一些单词小写

应该小写的单词是:
[“da,de,di,do,du,das,des,dis,dos,dus”]

所以,我现在的
code
是:

public static String capitalize(String word) {
   String newWord = WordUtils.capitalizeFully(word);
   newWord = newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase();
   return newWord;
}
  • 输入示例:

  • 何塞·达斯·席尔瓦
  • 乔治·德·保拉
  • 玛丽亚·达斯·保拉斯

问题是replaceAll将每个单词都放在小写,而不仅仅是与
模式匹配的介词

在使用正则表达式和
toLowerCase
之前,通过检查单词是否为目标来尝试设置条件

List<String> str = Arrays.asList("da, de, di, do, du, das, des, dis, dos, dus".split(", "));
newWord = str.contains(word) ? 
          newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase() : 
          newWord;
List str=Arrays.asList(“da、de、di、do、du、das、des、dis、dos、dus.split(“,”);
newWord=str.contains(单词)?
newWord.replaceAll(\\b([d|d][a-zA-Z]{1,2})\\b“,“$1”).toLowerCase():
新词;

在使用正则表达式和
toLowerCase

List<String> str = Arrays.asList("da, de, di, do, du, das, des, dis, dos, dus".split(", "));
newWord = str.contains(word) ? 
          newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase() : 
          newWord;
List str=Arrays.asList(“da、de、di、do、du、das、des、dis、dos、dus.split(“,”);
newWord=str.contains(单词)?
newWord.replaceAll(\\b([d|d][a-zA-Z]{1,2})\\b“,“$1”).toLowerCase():
新词;

无第三方LIB的Java8解决方案:

public static void main(String[] args) {
    String str = "hello mY dEAr friends";
    Set<String> ban = new HashSet<>(Arrays.asList("my", "dear"));
    String result = Arrays.stream(str.split("\\s"))
                          .map(s -> capitalize(s, ban))
                          .collect(Collectors.joining(" "));
    System.out.println(result);
}

static String capitalize(String s, Set<String> ban) {
    String lc = s.toLowerCase();
    return ban.contains(lc) ? lc 
                            : Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
}
publicstaticvoidmain(字符串[]args){
String str=“你好,我亲爱的朋友”;
Set ban=newhashset(Arrays.asList(“我的”、“亲爱的”);
字符串结果=Arrays.stream(str.split(“\\s”))
.map(s->capitalize(s,ban))
.collect(收集器。连接(“”);
系统输出打印项次(结果);
}
静态字符串大写(字符串s,设置禁止){
字符串lc=s.toLowerCase();
退货禁令。包含(lc)?lc
:Character.toUpperCase(s.charAt(0))+s.substring(1).toLowerCase();
}

无第三方LIB的Java8解决方案:

public static void main(String[] args) {
    String str = "hello mY dEAr friends";
    Set<String> ban = new HashSet<>(Arrays.asList("my", "dear"));
    String result = Arrays.stream(str.split("\\s"))
                          .map(s -> capitalize(s, ban))
                          .collect(Collectors.joining(" "));
    System.out.println(result);
}

static String capitalize(String s, Set<String> ban) {
    String lc = s.toLowerCase();
    return ban.contains(lc) ? lc 
                            : Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
}
publicstaticvoidmain(字符串[]args){
String str=“你好,我亲爱的朋友”;
Set ban=newhashset(Arrays.asList(“我的”、“亲爱的”);
字符串结果=Arrays.stream(str.split(“\\s”))
.map(s->capitalize(s,ban))
.collect(收集器。连接(“”);
系统输出打印项次(结果);
}
静态字符串大写(字符串s,设置禁止){
字符串lc=s.toLowerCase();
退货禁令。包含(lc)?lc
:Character.toUpperCase(s.charAt(0))+s.substring(1).toLowerCase();
}

那么您希望所有单词都大写,但您指定的单词除外? 或者您不希望任何单词大写,如果该单词与指定的其中一个匹配,则希望将其转换为小写

第一种情况: 您需要注意并确定是否要将 das 或任何包含该词的词,如 dasadada 如果仅与您指定的单词明确匹配,则

Str.matches("firstword|secondword");
或者任何包含这些词的词
Str.matches((*)第一个字(*)|(*)第二个字(*)

第二种情况:
那么您就不需要
String newWord=WordUtils.capitalizeFully(word)

那么您希望所有单词都大写,但您指定的单词除外? 或者您不希望任何单词大写,如果该单词与指定的其中一个匹配,则希望将其转换为小写

第一种情况: 您需要注意并确定是否要将 das 或任何包含该词的词,如 dasadada 如果仅与您指定的单词明确匹配,则

Str.matches("firstword|secondword");
或者任何包含这些词的词
Str.matches((*)第一个字(*)|(*)第二个字(*)

第二种情况:
那么您就不需要
String newWord=WordUtils.capitalizeFully(word)

您正在通过执行
newWord.replaceAll(\\b([d|d][a-zA-Z]{1,2})\\b,“$1”)将整个字符串转换为小写。您应该只将匹配项转换为小写

下面是代码段,它首先将输入字符串转换为大写,然后查找并将每个匹配项转换为小写

代码段:

public static void main(String[] args) {
    String str = "josé dAs sIlVa".toUpperCase();
    Matcher m = Pattern.compile("D(A|E|I|O|U|AS|ES|IS|OS|US)").matcher(str);

    while(m.find()) {
        String match = m.group(0);
        str = str.replace(match,match.toLowerCase());
    }

    System.out.println(str);
}
输入:

josé dAs sIlVa
输出:

JOSÉ daS SILVA

通过执行
newWord.replaceAll(\\b([d|d][a-zA-Z]{1,2})\\b“,“$1”)将整个字符串转换为小写。您应该只将匹配项转换为小写

下面是代码段,它首先将输入字符串转换为大写,然后查找并将每个匹配项转换为小写

代码段:

public static void main(String[] args) {
    String str = "josé dAs sIlVa".toUpperCase();
    Matcher m = Pattern.compile("D(A|E|I|O|U|AS|ES|IS|OS|US)").matcher(str);

    while(m.find()) {
        String match = m.group(0);
        str = str.replace(match,match.toLowerCase());
    }

    System.out.println(str);
}
输入:

josé dAs sIlVa
输出:

JOSÉ daS SILVA
class-MyClass
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
String[]wordArray={“jose dAs sIlVa”、“Jorge De PAuLa”、“MaRiA dAs PauLas”};
对于(int i=0;i
class MyClass
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
String[]wordArray={“jose dAs sIlVa”、“Jorge De PAuLa”、“MaRiA dAs PauLas”};

对于(int i=0;这正是<代码> > ToOrrCase](< /代码>)。您需要对每个匹配进行操作。因为要检查每个单词的匹配,请考虑拆分字符串,同时检查每个单词的匹配,而不是同时检查整个字符串。“当然,我知道什么是<代码> ToWORCaseCe()。
可以,但我只想将与
Regex
匹配的单词小写。那么您显然不知道表达式是如何工作的。这可能与
完全相同。toLowerCase()< /代码>。你需要操纵每一个匹配。既然你想检查每个单词的匹配,考虑拆分字符串,同时检查每个单词的匹配,而不是同时检查整个字符串。@当然,我知道什么是<代码> ToWORCaseCe()。
有,但我只想将与
正则表达式匹配的单词小写。那么您显然不知道表达式是如何工作的。可能会重复感谢您的回复。我同意