0

I am attempting to first match a string with a regex pattern and then use a second pattern to format that string. From what i've read up on, one way of achieving this is using .replaceAll() (edit: .replaceAll() is not used for this purpose, read comments on answer for clarification)

I have created this function whereby the aim is to:

  1. Match given string to match
  2. Format given string using format regex

    String match = "(^[A-Z]{2}[0-9]{2}[A-Z]{3}$)";
    String format = "(^[A-Z]{2}[0-9]{2}[*\\s\\\\][A-Z]{3}$)";
    String input = "YO11YOL"
    
    if (input.matches(match)) {
        return input.replaceAll(input, "??");
    }
    

The output should be YO11 YOL with a added space after the fourth character

3
  • If you're trying to replace the matches of format with something then you should use input.replaceAll(format, something). Commented Feb 27, 2019 at 21:39
  • I'm using the match string (regex) to check whether the input is valid and then my aim is to use the format string (regex) to format the input string. Commented Feb 27, 2019 at 21:42
  • AB12CDE becomes AB12 CDE or XY01ZAB becomes XY01 ZAB Commented Feb 27, 2019 at 21:46

2 Answers 2

3

This is what you want: Unfortunately it cannot be done the way that you want. But it can be done using substring.

public static void main(String args[]){
    String match = "(^[A-Z]{2}[0-9]{2}[A-Z]{3}$)";
    String input = "YO11YOL";

    if (input.matches(match)) {
       String out = input.substring(0, 4) + " " + input.substring(4, 7);
       System.out.println(out);
    }

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

7 Comments

What if i wanted to put a space after the first letter? So is becomes i s.
AH! You are misunderstanding replaceAll(). replaceAll does not replace one regex, with another. It replaces anything that matches the regex, with the replacement string.
the replacement string can not be a regex expression.
Thank you for clarifying, it makes sense now! What would be the most efficient way of formatting the string using a regex?
I've edited to show you how to do it for the string YO11YOL. But it will only work for strings that are formatted the same. Youll have to modify it for strings that dont pass the regex you supplied.
|
0

You may use two capturing groups and refer to them from the string replacement pattern with placeholders $1 and $2:

String result = input.replaceFirst("^([A-Z]{2}[0-9]{2})([A-Z]{3})$", "$1 $2");

See the regex demo and a diagram:

enter image description here

Note that .replaceFirst is enough (though you may also use .replaceAll) as there is only one replacement operation expected (the regex matches the whole string due to the ^ and $ anchors).

2 Comments

This is the answer i was looking for, so if i were to add another capturing group, i'd just need to add an additional placeholder? So $1 $2 becomes $1 $2 $3 ?
@Sha-1 Yes, e.g. to get YO 11 YOL use input.replaceFirst("^([A-Z]{2})([0-9]{2})([A-Z]{3})$", "$1 $2 $3")

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.