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:
- Match given string to
match Format given string using
formatregexString 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

formatwith something then you should useinput.replaceAll(format, something).matchstring (regex) to check whether the input is valid and then my aim is to use theformatstring (regex) to format the input string.