Moonstone
Moonstone

Reputation: 121

Regex to remove spaces inside quoted text

I need to remove all white space only within quoted sections of a string.

Give this: 10 00,400,"a1 b2 c3 ",zz xx,100

I need this: 10 00,400,"a1b2c3",zz xx,100

Obviously, restricting it to quoted areas only is why I'm having trouble.

The strings will vary in length and can have multiple quoted sections.

Upvotes: 2

Views: 1927

Answers (3)

Chip
Chip

Reputation: 3316

Doesn't use regex - but works

public String replaceWithinQuotes(String input) {
    String[] output = input.split("\"");
    StringBuilder builder = new StringBuilder();
    for ( int i =0; i < output.length-1; i++ ) {
        if ( i %2 == 0 ) {
            builder.append(output[i]);
        } else {
            builder.append(output[i].replaceAll("[ ]+", ""));
        }
        builder.append("\"");
    }
    builder.append(output[output.length-1]);
    return builder.toString();
}

Note - If you are using this - make sure length of the array is odd. If it is not, then you have unbalanced quotes and you have to handle that in whatever way is appropriate for your application.

Upvotes: 2

rlinden
rlinden

Reputation: 2041

Here is a small routine that seems to work just fine when there is a single set of quotes in the text:

public static String cropSpacesWithinQuotes(String expression) {
    Pattern pattern = Pattern.compile("\"[\\S*\\s\\S*]*\"");
    StringBuilder noSpaces=new StringBuilder();
    int initialPosition=0;
    Matcher matcher = pattern.matcher(expression);
    while (matcher.find(initialPosition)) {
            int pos=matcher.start();
            noSpaces.append(expression.substring(initialPosition, pos-initialPosition));
            initialPosition=matcher.end();
            noSpaces.append(matcher.group().replaceAll(" ", ""));
    }
    noSpaces.append(expression.substring(initialPosition));
    return(noSpaces.toString());
}

Performing some unit tests I realized that when there is more that one pair of quotes the text within the two sets also has its spaces cropped. Some manipulation on the variable initialPosition should solve your problem.

I hope this helps.

Upvotes: 1

Paul Vargas
Paul Vargas

Reputation: 42020

Assuming that the quotes are balanced, then you could implement a method like this:

public static void main(String[] args) {

    String str = "10 00,400,\"a1 b2 c3 \",zz xx,100, \"a b\"";
    StringBuffer sb = new StringBuffer();

    Matcher matcher = Pattern.compile("\"([^\"]+)\"").matcher(str);
    while (matcher.find()) {
        matcher.appendReplacement(sb, matcher.group().replaceAll("\\s+", ""));
    }

    System.out.println(sb.toString());
}

This prints:

10 00,400,"a1b2c3",zz xx,100, "ab"

Upvotes: 1

Related Questions