开发者社区> 问答> 正文

如何使用regex、Java查找换行后开始的单词?

我有一个输入字符串,由几行组成,例如:

When I was younger I never needed And I was always OK but it was a long Time Ago 问题是将长度大于3的所有单词的首字母反转。即输出必须是以下内容:

when I Was Younger I Never Needed and I Was Always OK But it Was a Long time ago 代码:

import java.util.regex.*;

public class Part3_1 {

public static void main(String[] args) {
    String str = "When I was younger\r\nI never needed\r\nAnd I was always OK\r\nbut it was a long Time Ago";
    System.out.println(convert(str));

}

public static String convert(String str) {
    String result = "";
    String[] strings = str.split(" ");
    String regexLowerCase = "\\b[a-z]{3,}\\b";
    String regexLowerCaseInitial = "(\\r\\n)[a-z]{3,}\\b";
    String regexUpperCase = "\\b([A-Z][a-z]{2,})+\\b";
    String regexUpperCaseInitial = "(\\r\\n)([A-Z][a-z]{2,})\\b";
    Pattern patternLowerCase = Pattern.compile(regexLowerCase, Pattern.MULTILINE);
    Pattern patternUpperCase = Pattern.compile(regexUpperCase, Pattern.MULTILINE);
    Pattern patternLowerCaseInitial = Pattern.compile(regexLowerCaseInitial, Pattern.MULTILINE);
    Pattern patternUpperCaseInitial = Pattern.compile(regexUpperCaseInitial, Pattern.MULTILINE);

    for (int i = 0; i < strings.length; i++) {
        Matcher matcherLowerCase = patternLowerCase.matcher(strings[i]);
        Matcher matcherUpperCase = patternUpperCase.matcher(strings[i]);
        Matcher matcherLowerCaseInitial = patternLowerCaseInitial.matcher(strings[i]);
        Matcher matcherUpperCaseInitial = patternUpperCaseInitial.matcher(strings[i]);
        char[] words = strings[i].toCharArray();
        if (matcherLowerCase.find() || matcherLowerCaseInitial.find()) {
            char temp = Character.toUpperCase(words[0]);
            words[0] = temp;
            result += new String(words);
        } else if (matcherUpperCase.find() || matcherUpperCaseInitial.find()) {
            char temp = Character.toLowerCase(words[0]);
            words[0] = temp;
            result += new String(words);
        } else {
            result += new String(words);
        }

        if (i < strings.length - 1) { 
            result += " "; 
        } 
    }
    return result;
}

} 这里:

"\b[a-z]{3,}\b" 是一个正则表达式,选择所有小写且长度为3个或更多符号的单词, "\b([A-Z][a-z]{2,})+\b" 是一个正则表达式,选择所有长度从3个或更多符号开始的大写字母。 这两个正则表达式都可以正常工作,但是当我们有换行符时,它们不起作用。我的程序执行的输出如下:

when I Was Younger I Never Needed And I Was Always OK but it Was a Long Time ago 据我所知,这些正则表达式不能分别从required \r\nAnd和OK\r\nbut中选择单词和And和but。

要解决此错误,我尝试添加新的正则表达式"(\r\n)[a-z]{3,}\b"和"(\r\n)([A-Z][a-z]{2,})\b",但它们不起作用。

如何组成正则表达式,在换行后选择单词?

展开
收起
小六码奴 2019-10-09 17:07:27 791 0
1 条回答
写回答
取消 提交回答
  • 一种选择是改为用分行符(\b)分割字符串,然后将空格传递到strings数组中的最后一个字符串。这消除了针对不同情况使用单独的正则表达式的需要,也消除了添加退格字符的需要。这将为您提供所需的结果:

    public static String convert(String str) { String result = ""; String[] strings = str.split("\b"); String regexLowerCase = "^[a-z]{3,}"; String regexUpperCase = "^[A-Z][a-z]{2,}+"; Pattern patternLowerCase = Pattern.compile(regexLowerCase, Pattern.MULTILINE); Pattern patternUpperCase = Pattern.compile(regexUpperCase, Pattern.MULTILINE);

    for (int i = 0; i < strings.length; i++) {
        Matcher matcherLowerCase = patternLowerCase.matcher(strings[i]);
        Matcher matcherUpperCase = patternUpperCase.matcher(strings[i]);
        char[] words = strings[i].toCharArray();
        if (matcherLowerCase.find()) {
            char temp = Character.toUpperCase(words[0]);
            words[0] = temp;
            result += new String(words);
        } else if (matcherUpperCase.find()) {
            char temp = Character.toLowerCase(words[0]);
            words[0] = temp;
            result += new String(words);
        } else {
            result += new String(words);
        }
    
    }
    return result;
    

    } 输出:

    when I Was Younger I Never Needed and I Was Always OK But it Was a Long time ago

    2019-10-09 17:26:11
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载