0

The class UpperCaseConverter converts the input text into uppercase text excluding the word matched to regEx "(?i)^((?!sht).)*".

What I expected to get is 1A0A-50-CRD-140-20002-063484C-N-01_Sht_1A.pdf but the result is 1A0A-50-CRD-140-20002-063484C-N-01_Sht_1a.pdf.

How could the last part of the letter be uppercase except for the .pdf

UpperCaseConverter

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UpperCaseConverter {

    private UpperCaseConverter(){};

    public static String convert(String text, String regEx) {
        StringBuilder toBeConverted = new StringBuilder(text);

        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(toBeConverted);

        while(matcher.find()) {
            String buf = toBeConverted.substring(matcher.start(), matcher.end()).toUpperCase();
            toBeConverted.replace(matcher.start(), matcher.end(), buf);
        }

        return toBeConverted.toString();
    }
}

*TestCase

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class UpperCaseConverterTest {

    @Test
    public void testUpperCase() {
        String expect = UpperCaseConverter.convert("1a0A-50-CRD-140-20002-063484C-N-01_Sht_1a.pdf", 
                "(?i)^((?!sht).)*");
        assertEquals("1A0A-50-CRD-140-20002-063484C-N-01_Sht_1A.pdf", expect);
    }
}
4

1 回答 1

0

根据您的指示,您的正则表达式在最后一次出现“sht”之前停止。只有正则表达式部分得到大写处理。

于 2013-07-27T23:21:23.587 回答