- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
任务:
The task must be solved using regular expressions without using container classes.
Input: text (may consist of Latin and Cyrillic). Output: source text, but the case of the first character of each word, which consists of three or more characters, must be inverted.
To consider a word as a sequence containing only letters (all other characters are not included in the word). Create a static convert method that converts input to output.
Example Input data
When I was younger
I never needed
Прощай, со всех вокзалов поезда
уходят в Дальние КраяExample Output
when I Was Younger
I Never Needed
прощай, со Всех Вокзалов Поезда
Уходят в дальние края
我的尝试:
public static String convert(String input) {
StringBuilder sb = new StringBuilder(input);
Pattern p = Pattern.compile("[\\W&&[\\d]]?[\\w&&[\\D]]+");
Matcher m = p.matcher(input);
while (m.find()) {
if (m.group().length() >= 3) {
if (Character.isUpperCase(sb.charAt(m.start()))) {
sb.setCharAt(m.start(), Character.toLowerCase(sb.charAt(m.start())));
} else {
sb.setCharAt(m.start(), Character.toUpperCase(sb.charAt(m.start())));
}
}
}
return sb.toString();
}
我需要输出:
when I Was Younger
I Never Needed
прощай, со Всех Вокзалов Поезда
Уходят в дальние края
但我有:
when I Was Younger
I Never Needed
Прощай, со всех вокзалов поезда
уходят в Дальние Края
最佳答案
\w
与西里尔字符不匹配。我通过在 while 循环中打印匹配的组来解决这个问题:
System.out.println(m.group());
打印:
When
I
was
younger
I
never
needed
没有其他单词匹配。
要匹配西里尔字符,您可以使用 \p{L}
。如果您使用 {3}
匹配三个字符,则可以避免在循环中进行长度检查。 \b
匹配边界字符。把它们放在一起:
public static String convert(String input) {
StringBuilder sb = new StringBuilder(input);
Pattern p = Pattern.compile("\\b\\p{L}{3}");
Matcher m = p.matcher(input);
while (m.find()) {
char firstChar = sb.charAt(m.start());
if (Character.isUpperCase(firstChar)) {
sb.setCharAt(m.start(), Character.toLowerCase(firstChar));
} else {
sb.setCharAt(m.start(), Character.toUpperCase(firstChar));
}
}
return sb.toString();
}
产品:
when I Was Younger
I Never Needed
прощай, со Всех Вокзалов Поезда
Уходят в дальние края
或者,如果您想要真正漂亮,请使用正向前瞻(非捕获组)和采用 lambda 的匹配器 replaceAll
方法:
public static String convert(String input) {
Pattern p = Pattern.compile("\\b(\\p{L})(?=\\p{L}{2})");
Matcher m = p.matcher(input);
return m.replaceAll(match -> {
char ch = match.group().charAt(0);
if (Character.isUpperCase(ch)) {
return "" + Character.toLowerCase(ch);
}
return "" + Character.toUpperCase(ch);
});
}
还生产:
when I Was Younger
I Never Needed
прощай, со Всех Вокзалов Поезда
Уходят в дальние края
关于java - 正则表达式 - 无法将西里尔字母与\w匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58226125/
我在数据库中有一个具有俄罗斯值(value)观的项目。我需要做的就是附和他们,但事实证明这比预期的要困难。所有俄语字符都只是作为问号打印。 IE: ??? ? ????????对于我尝试过的每种编码,
这是我解析 JSON 的函数: func jsonParsingWeather(urlPath:String) -> NSDictionary { var utf8URLPath = urlP
我是一名优秀的程序员,十分优秀!