Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.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 正则表达式:要更改集合之一后面的字母大小写,HTML实体除外_Java_Regex - Fatal编程技术网

Java 正则表达式:要更改集合之一后面的字母大小写,HTML实体除外

Java 正则表达式:要更改集合之一后面的字母大小写,HTML实体除外,java,regex,Java,Regex,示例: rythm&blues -> Rythm&Blues .. DON'T WEAR WHITE/LIVE -> Don't Wear White/Live 首先,我将整个字符串转换为小写(因为我希望单词的开头只有大写) 我目前使用拆分模式来实现这一点:[&/\\.\\s-] 然后我把零件的第一个字母转换成大写 它工作得很好,除了它还转换HTML实体之外,当然: 例如,don'

示例:

rythm&blues                   -> Rythm&Blues  
.. DON'T WEAR WHITE/LIVE -> Don't Wear White/Live
首先,我将整个字符串转换为小写(因为我希望单词的开头只有大写)

我目前使用拆分模式来实现这一点:
[&/\\.\\s-]
然后我把零件的第一个字母转换成大写

它工作得很好,除了它还转换HTML实体之外,当然: 例如,
don't
转换为
don&Apos;t
但该实体应单独存在

在写这篇文章时,我发现了另外一个问题。。。最初转换为小写可能也会弄乱一些HTML实体。因此,实体应该完全独立。(例如,
Ç;
Ç;
不同)

HTML实体可能是这样匹配的:
&[a-z][a-z][a-z]{1,5}

我正在考虑对组做一些事情,但不幸的是,我发现很难弄清楚。

这里的解决方案可能是前瞻性断言。这意味着拆分仅当不是实体的开始时才应匹配
&
字符。这里的问题是,我不确定您的数据是否可以包含文本,这些文本可能会被误认为是一个实体(基本上是以
结尾的任何内容)。但现在我们假设它不是。这就是这种具有负前瞻模式的拆分的方式:

/(?!')[&/\.\s-]/
注意:这是一个只有
&apos实体。您可能希望扩展可能的实体列表或提供匹配所有有效实体的模式


这里有一个技巧(JS,但也应该在Java中工作):

这个模式似乎可以处理您的情况

"\\w+|&#?\\w+;\\w*"
可能存在一些边缘情况,但我们可以在出现边缘情况时进行相应调整

模式分解:

  • \\w+
    -匹配任何单词
  • &#?\\w+\\w*
    -匹配HTML实体
代码示例:

public static void main(String[] args) throws Exception {
    String[] lines = {
        "rythm&blues",
        ".. DON'T WEAR WHITE/LIVE"
    };

    Pattern pattern = Pattern.compile("\\w+|&#?\\w+;\\w*");
    for (int i = 0; i < lines.length; i++) {
        Matcher matcher = pattern.matcher(lines[i]);
        while (matcher.find()) {
            if (matcher.group().startsWith("&")) {
                // Handle HTML entities 

                // There are letters after the semi-colon that 
                // need to be lower case
                if (!matcher.group().endsWith(";")) {
                    String htmlEntity = matcher.group();
                    int semicolonIndex = htmlEntity.indexOf(";");
                    lines[i] = lines[i].replace(htmlEntity,
                            htmlEntity.substring(0, semicolonIndex) +
                                    htmlEntity.substring(semicolonIndex + 1)
                                            .toLowerCase());
                }
            } else {
                // Uppercase the first letter of the word and lowercase
                // the rest of the word
                lines[i] = lines[i].replace(matcher.group(), 
                        Character.toUpperCase(matcher.group().charAt(0)) + 
                                matcher.group().substring(1).toLowerCase());
            }
        }
    }

    System.out.println(Arrays.toString(lines));
}

您是否有一个可以在您的数据中找到的实体列表,或者它应该考虑所有可能的实体?问题的第4段特别提到,整个文本小写是不可接受的。我也不认为一个团队可以(或应该)从
开始字符。话虽如此,回答得很好,我开始写类似的东西,问题应该很容易解决。哦,根据w3,HTML实体也可以用
#NUMBER
来引用,而不是名称(比如
<;
),正则表达式也应该反映出这一点。@Deltharis感谢您的帮助!更新的答案。非常满意这个解决方案。工作起来很有魅力。我不得不做一个小改动:htmlenty。子字符串(0,分号索引)应该是htmlenty。子字符串(0,分号索引+1)@dexter很高兴能帮上忙。请点击我答案的复选标记,这样你的问题就解决了。
[Rythm&Blues, .. Don&apos;t Wear White/Live]