Java中的ReplaceAll

时间:2016-03-06 15:15:52

标签: java regex replaceall

我正在使用WordsUtilscapitalize个字词。

由于我无法定义哪些单词应该大写,我必须在大写函数之后再进行另一个实现,以便将一些单词设置为小写。

应为小写的字词为:["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;
}
  • 输入示例:

    1. josédAssIlVa
    2. Jorge De PAuLa
    3. MaRiA DAS PauLas

问题是,replaceAll会将每个单词都小写,而不仅仅是与Pattern匹配的介词。

5 个答案:

答案 0 :(得分:4)

没有第三方库的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();
}

答案 1 :(得分:0)

在使用正则表达式和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;

答案 2 :(得分:0)

所以你希望所有单词都被大写,但是你指定的单词? 或者你想要没有单词大写,如果单词与指定的单词匹配,那么你想将它转换为小写?

第一个案例: 你需要注意并确定你是否要小写字母 的 DAS 或任何包含该词的单词 的 dasadada 如果只是专门匹配你指定的单词

$phoneNumber= file_get_contents("https://page.company.test/?app=".$appid."&affiliateid=".$affiliatep."&laNG=en&country=".$countryp."&sav=".$savactive)

或包含这些词的任何单词    Str.matches("firstword|secondword");

第二种情况: 那么你不需要Str.matches("(.*)firstword(.*)|(.*)secondword(.*)");

答案 3 :(得分:0)

您正在通过newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase();将整个字符串转换为小写字母。你应该只将匹配转换为小写。

下面是代码片段,它首先将输入字符串转换为大写字母,然后查找&amp;将每个匹配转换为小写。

代码段:

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

答案 4 :(得分:0)

class MyClass
{
    public static void main (String[] args) throws java.lang.Exception
    {

       String[] wordArray = {"jose dAs sIlVa","Jorge De PAuLa","MaRiA DAS PauLas"};
       for(int i=0;i<wordArray.length;i++){
        System.out.println(capitalize(wordArray[i]));   
       }
     }

    static String capitalize(String word) {
             if(word!=null && word!=""){
               String[] wordArray = word.trim().split(" ");
               word= "";
               for(int i=0;i<wordArray.length;i++){
                   String currentWord = wordArray[i].trim();
                   if(currentWord.matches("\\b([d|D][a-zA-Z]{1,2})\\b")){
                       currentWord = currentWord.toLowerCase();
                   }else{
                       currentWord = currentWord.toUpperCase();
                   }
                   word = word+" "+currentWord;
               }
           }
           return word.trim();
        }
}

输出:

JOSE das SILVA

JORGE de PAULA

MARIA das PAULAS