我有一个输入字符串,由几行组成,例如:
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",但它们不起作用。
如何组成正则表达式,在换行后选择单词?
一种选择是改为用分行符(\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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。