2
txt.replaceAll("a","b"); 

Is "a" a Char Sequence or a Regex (or more specific Literal Search)?

And is my code correct? I’m coding the Exercise "Normalize Text". Task:

  • Only one space between words.
  • Only one space after comma (,), dot (.) and colon (:). First character of word after dot is in Uppercase and other words are in lower case.

Please correct me if I am wrong, including my English.

public class NormalizeText {

    static String spacesBetweenWords(String txt){
        txt = txt.replaceAll(" +", " ");
        return txt;
    }

    /**
     * - There are no spaces between comma or dot and word in front of it. 
     * - Only one space after comma (,), dot (.) and colon (:).
     */
    static String spacesCommaDotColon(String txt) {
        txt = txt.replaceAll(" +\\.", ".");
        txt = txt.replaceAll(" +,", ",");
        txt = txt.replaceAll(" +[:]", ":");
        txt = txt.replaceAll("[.]( *)", ". ");
        txt = txt.replaceAll("[,]( *)", ", ");
        txt = txt.replaceAll("[:]( *)", ": ");
        //txt.replaceAll("a","b");

        return txt;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        String txt = "\" \\\"     i want to   f\\\"ly\"  .  B.ut :  I   ,     Cant\\";
        System.out.println(txt);
        txt = spacesBetweenWords(txt);
        System.out.println(spacesBetweenWords(txt));
        System.out.println(spacesCommaDotColon(txt));
    }

}

My teacher said my code is not using regex, but rather a Char Sequence. I am very confused.

5
  • 4
    technically "a" is a regex (albeit a trivial regex) and a character sequence. I think your professor is trying to get you to use a more complicated regex, instead of a sequence of trivial ones. Commented Oct 3, 2019 at 13:09
  • If you do not want to use regex you would use txt.replace("a","b"); instead of replaceAll, replaceAll is always regex. Note that replace still replaces all occurrences. Commented Oct 3, 2019 at 13:11
  • Are you asking about the method? Or your method call? Commented Oct 3, 2019 at 13:12
  • Raedwald: the spacesCommaDotColon(); Method Commented Oct 3, 2019 at 13:17
  • 1
    @Gus your comment is the answer :D Commented Oct 3, 2019 at 13:26

3 Answers 3

1

For starters because you learn how to user regex, an amazing site to learn how to use regex is this. Now replaceAll first argument counts as regex. Just the letter "a" is a regex matching only the "a" inside the text. So what your teacher meant is probably to use a more complicated regex ( something to match multiple cases at once). As this is an exercise I prefer not to give a solution so you will try to figure it out by yourself. The tip is try to use replaceAll only once.! Or the closer you can get to once.

As for your code if its correct. It seems good but you are missing the uppercase after the dots condition. Also because I said try to use only one replaceAll the solution for the uppercase doesn't count as it requires an other approach.

I hope I helped and you will find a solution to the exercise and again sorry for not providing an answer to the exercise but In my opinion you need to try to figure it out on your own. You are already on a good road!

Sign up to request clarification or add additional context in comments.

1 Comment

@Amessihel your answer is not wrong just not on the teaching spec that this question seem to have, same as the followup comments :D
0

With regards to replaceAll, the docs say:

Replaces each substring of this string that matches the given regular expression with the given replacement.

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

        Pattern.compile(regex).matcher(str).replaceAll(repl)

Therefore, replaceAll will always use regular expressions for its first parameter. With regards to simplifying your code,

static String spacesCommaDotColon(String txt) {
    txt = txt.replaceAll(" +\\.", ".");
    txt = txt.replaceAll(" +,", ",");
    txt = txt.replaceAll(" +[:]", ":");
    txt = txt.replaceAll("[.]( *)", ". ");
    txt = txt.replaceAll("[,]( *)", ", ");
    txt = txt.replaceAll("[:]( *)", ": ");
    //txt.replaceAll("a","b");

    return txt;
}

can be simplified to:

static String spacesCommaDotColon(String txt) {
    return txt.replaceAll(" *([:,.]) *","$2 ");
}

and

static String spacesBetweenWords(String txt){
    txt = txt.replaceAll(" +", " ");
    return txt;
}

can be simplified to:

static String spacesBetweenWords(String txt){
    return txt.replaceAll(" +", " ");
}

Comments

0

Your code is correct. Also, you could perform dot, comma and colon formatting with one call using capturing groups:

static String spacesCommaDotColon(String txt) {
    return txt.replaceAll("\\s*([.,:])\\s*", "$1 ");
}

Explanation:

  • "\\s*([.,:])\\s*": look for a comma, dot or colon character with any surrounding blank character; capture said character (parenthesis captures matched text)
  • "$1 ": replace the matched text by the captured character (labelled as $1 since it's was caught by the first and uniq capturing parenthesis) and one space

Another solution given by TEXHIK, using look-ahead:

txt.replaceAll("(?<=[,.:])\s{2,}", "");

Which looks for any set of at least two blank character preceded by a comma, a dot or a colon and remove it. Maybe not something to see before understanding regex basis.

2 Comments

It is also possible to use look-behind, java regex supports it as I know: txt.replaceAll("(?<=[,.:])\s{2,}", ""); Add it to your answer to have all ways in one place.
Yes, I can, but I usually don't read other answers after one, that covers my case and have good formatting and explanation. So not all well see this way in my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.