admin管理员组

文章数量:1122846

Over the last couple of days I have read through many similar questions but none that actually answers what I am looking for. Hence , posting this question.

I have a string, for example:

    String myString = "Rule2 = if(event = 'Veteran''s Day', 'Day 1', 'Day 2', Measure1.Lag)"; 

I want this to be changed to the following:

    myString = "RULE2=IF(EVENT='Veteran''s Day','Day 1','Day 2', MEASURE1.LAG)";

That is all text except those within single quotes are trimmed of extra spaces and turned to upperCase().

I thought I could use regex to extract [Rule2,if,event] and use String.replaceAll() et al. No luck. It is the third day today. My head has stopped working now. Please help.

Would be grateful for any pointers.

Over the last couple of days I have read through many similar questions but none that actually answers what I am looking for. Hence , posting this question.

I have a string, for example:

    String myString = "Rule2 = if(event = 'Veteran''s Day', 'Day 1', 'Day 2', Measure1.Lag)"; 

I want this to be changed to the following:

    myString = "RULE2=IF(EVENT='Veteran''s Day','Day 1','Day 2', MEASURE1.LAG)";

That is all text except those within single quotes are trimmed of extra spaces and turned to upperCase().

I thought I could use regex to extract [Rule2,if,event] and use String.replaceAll() et al. No luck. It is the third day today. My head has stopped working now. Please help.

Would be grateful for any pointers.

Share Improve this question edited Nov 27, 2024 at 10:30 5122014009 asked Nov 22, 2024 at 11:33 51220140095122014009 4,0927 gold badges26 silver badges35 bronze badges 3
  • 3 Space after comma should also be trimmed right? – anubhava Commented Nov 22, 2024 at 11:43
  • 1 @anubhava yes. Updated the example. Thanks! – 5122014009 Commented Nov 27, 2024 at 10:27
  • ok I think you still missed space after'Day 2', in the expected output. I have posted a solution below, see if that works out for you. – anubhava Commented Nov 27, 2024 at 11:24
Add a comment  | 

5 Answers 5

Reset to default 3

Here I use a a pattern for '...' or '... or non-apostrophes. Then that replaceAll will work, alternating uppercasing parts.

Pattern pattern = Pattern.compile("('[^']*('|$)|[^']+)");
String myString = "Rule2 = if(event = 'Veteran''s Day', 'Day1', 'Day2')";
Matcher m = pattern.matcher(myString);
String s = m.replaceAll(mr -> mr.group().startsWith("'")
                              ? mr.group() : mr.group().toUpperCase());

Shorter would be '.*?' but this is clear and does not clash with an odd number of apostrophes

I think using the Pattern class along with Regex would solve your problem.

Below is the example implementation:

// Regular expression to match text within single quotes
Pattern pattern = Pattern.compile("'(.*?)'"); // The java.util.regex.Pattern class
Matcher matcher = pattern.matcher(input); // The java.util.regex.Matcher class

StringBuilder result = new StringBuilder();
int lastIndex = 0;

while (matcher.find()) {
    String outsideQuotes = input.substring(lastIndex, matcher.start());
    result.append(outsideQuotes.replaceAll("\\s+", "").toUpperCase());

    result.append(input.substring(matcher.start(), matcher.end()));

    lastIndex = matcher.end();
}

// Append the remaining text after the last match
if (lastIndex < input.length()) {
    result.append(input.substring(lastIndex).replaceAll("\\s+", "").toUpperCase());
}

// result.toString() is the final String result.

Hope this helps.

You may consider following regex based solution for your task:

final Pattern QRX = Pattern.compile("'[^']*'|([^']+)");

String s = "Rule2 = if(event = 'Veteran''s Day', 'Day 1', 'Day 2', Measure1.Lag)";
s = QRX.matcher(s).replaceAll(m ->
   m.group(1) != null ? m.group().replace(" ", "").toUpperCase() : m.group()
);
System.out.println(s);
//=> RULE2=IF(EVENT='Veteran''s Day','Day 1','Day 2',MEASURE1.LAG)

Regex pattern '[^']*'|([^']+) matches either a single quoted string or any 1+ characters that doesn't contain a ' in the 1st capture group.

Code Demo

Here is one way.

The data

String text = "Rule2 = if(event = 'Veteran''s Day', 'Day1', 'Day2')";
String desiredResult = "RULE2=IF(EVENT='Veteran''s Day', 'Day1', 'Day2')";

The process

text = text.replaceAll("'.*|\\s", "").toUpperCase()
        + text.substring(text.indexOf('\''));

The result

System.out.println(text);
System.out.println(text.equals(desiredResult));

Prints

RULE2=IF(EVENT='Veteran''s Day', 'Day1', 'Day2')
true

The Explanation

  • Remove everything from the first single quote onward, removing white space and converting to uppercase.
  • Prepend that to the substring beginning with the first single quote.

In JDK21 you might make use of splitWithDelimiters which pulls out all quoted components to an array. Then you can replace all spaces in even index positions and re-join each component:

static String reformat(String s) {
    var arr = s.splitWithDelimiters("'(.*?)'", -1);
    for (int i = 0; i< arr.length; i+=2) {
        arr[i]=arr[i].replace(" ", "").toUpperCase();
    }
    return String.join("", arr);
}

The above can be re-worked to one liner:

static String reformat(String s) {
    return Arrays.stream(s.splitWithDelimiters("'(.*?)'", -1))
          .map(x -> x.startsWith("'") ? x : x.replace(" ", "").toUpperCase())
          .collect(Collectors.joining());
}

本文标签: javaChange toUpperCase() all except those within single quotesStack Overflow