Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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/19.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正则表达式:用一定数量的字符大写单词_Java_Regex_String_Character - Fatal编程技术网

java正则表达式:用一定数量的字符大写单词

java正则表达式:用一定数量的字符大写单词,java,regex,string,character,Java,Regex,String,Character,我正在尝试将一个超过5个字符的字符串中的单词大写 我能够使用.length检索大于5个字符的单词数,并且我可以排除大于5个字符的单词,但我无法将它们大写 例输入:“我喜欢吃馅饼” 输出:“我喜欢吃馅饼” 这是我的密码: public static void main(String[] args) { String sentence = ""; Scanner input = new Scanner(System.in); System.out.println("Ent

我正在尝试将一个超过5个字符的字符串中的单词大写

我能够使用.length检索大于5个字符的单词数,并且我可以排除大于5个字符的单词,但我无法将它们大写

例输入:“我喜欢吃馅饼”

输出:“我喜欢吃馅饼”

这是我的密码:

 public static void main(String[] args) {
    String sentence = "";
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a sentence: ");
    sentence = input.nextLine();
    String[] myString = sentence.split("\\s\\w{6,}".toUpperCase());
     for (String myStrings : myString) {
         System.out.println(sentence);
         System.out.println(myStrings);
         }

以空格作为分隔符拆分输入句子,如果长度大于5,则使用intiCap方法:

PS:System.out.print将替换为StringBuilder

String delim = " ";
String[] myString = sentence.split(delim);

for (int i = 0; i < myString.length; i++) {
    if (i != 0) System.out.print(delim);

    if (myString[i].length() > 5)
        System.out.print(intiCap(myString[i]));
    else
        System.out.print(myString[i]);
}


private static String intiCap(String string) {
    return Character.toUpperCase(string.charAt(0)) + string.substring(1);
}
String delim=”“;
String[]myString=句子.split(delim);
for(int i=0;i5)
系统输出打印(intiCap(myString[i]);
其他的
System.out.print(myString[i]);
}
私有静态字符串(字符串){
返回字符.toUpperCase(string.charAt(0))+string.substring(1);
}
字符串句子=”;
StringBuilder sb=新的StringBuilder(句子长度());
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个句子:”);
句子=input.nextLine();
/*
*\\s(匹配空白字符)
*(您可以使用以下内容(简短而甜蜜:p):


请参见

您可以使用回调进行查找和替换。
”(?编辑:我尝试使用“全部替换”,现在我可以将任何大于5个字符的单词大写,但我只想将第一个字母字符串myString=句子.replaceAll(“\\b\\w{1,6}\\b\\s?”,”).toUpperCase();System.out.println(句子);System.out.println(myString);对于固定数量的字符,它的
\w{num}
。我的正则表达式只将单词的第一部分与固定的最小字符数匹配,不需要匹配整个内容。如果您只想替换第一个字母,它也只会查找以小写字母开头的单词,等等。
(?是一个典型的空格单词样式边界,它也与字符串的请求相匹配(如
\b
所做的)。我刚刚添加了子字符串,由于字符串myString=句子.replaceAll(“\\b\\w{1,6}\\b\\s?”,”);字符串=myString.substring(0,1).toUpperCase()+myString.substring(1);System.out.println(句子);System.out.println(上);谢谢,我想出来了,但是你的方法是一个很好的替代方法。这帮助我了解了很多。我知道replaceAll将删除少于5个字符的单词,但是有没有其他方法可以保留少于5个字符的单词,并将大于5个字符的单词大写。这就是我的:字符串myString=句子.replaceAll(“\\b\\w{1,6}\\b\\s?”,”);字符串=myString.substring(0,1).toUpperCase()+myString.substring(1);System.out.println(句子);System.out.println(大写);如果没有更简单的方法,我将使用模式和匹配器我知道没有简单的解决方案,但我可以编辑我的答案,以备选择
String sentence = "";
StringBuilder sb = new StringBuilder(sentence.length());
Scanner input = new Scanner(System.in);

System.out.println("Enter a sentence: ");
sentence = input.nextLine();
/*
 * \\s (match whitespace character)
 * (<?g1> (named group with name g1)
 * \\w{6,}) (match word of length 6) (end of g1)
 * | (or)
 * (?<g2> (named group with name g2)
 * \\S+) (match any non-whitespace characters) (end of g2)
 */
Pattern pattern = Pattern.compile("\\s(?<g1>\\w{6,})|(?<g2>\\S+)");
Matcher matcher = pattern.matcher(sentence);

//check if the matcher found a match
while (matcher.find())
{
    //get value from g1 group (null if not found)
    String g1 = matcher.group("g1");
    //get value from g2 group (null if not found)
    String g2 = matcher.group("g2");

    //if g1 is not null and is not an empty string
    if (g1 != null && g1.length() > 0)
    {
        //get the first character of this word and upercase it then append it to the StringBuilder
        sb.append(Character.toUpperCase(g1.charAt(0)));
        //sanity check to stop us from getting IndexOutOfBoundsException
        //check if g1 length is more than 1 and append the rest of the word to the StringBuilder
        if(g1.length() > 1) sb.append(g1.substring(1, g1.length()));
        //append a space
        sb.append(" ");
    }
    //we only need to check if g2 is not null here
    if (g2 != null)
    {
        //g2 is smaller than 5 characters so just append it to the StringBuilder
        sb.append(g2);
        //append a space
        sb.append(" ");
    }
}
System.out.println("Original Sentence: " + sentence);
System.out.println("Modified Sentence: " + sb.toString());
Pattern p = Pattern.compile("(?=\\b\\w{6,})([a-z])\\w+");
Matcher m = p.matcher(sentence);
StringBuffer s = new StringBuffer();
while (m.find()){
    m.appendReplacement(s, m.group(1).toUpperCase() + m.group(0).substring(1));
}
System.out.println(s.toString());