在 Java 中,我想在字符串后插入一个空格,但前提是该字符串包含“MAVERICK”。我认为使用使用正则表达式作为参数的 replaceAll() 方法可以做到这一点,但我并没有真正理解它。
这是我的
String s = "MAVERICKA";
//the last character can be from the following set [A,Z,T,SE,EX]
因此,我希望函数返回字符串“MAVERICK A”或“MAVERICK EX”。 例如。
- 特立独行 -> 特立独行 A
- 特立独行 -> 特立独行 EX
此外,如果字符串的格式已经正确,则不应插入空格。即
- 特立独行 A -> 特立独行 A
最佳答案
怎么样
s = s.replaceAll("MAVERICK(A|Z|T|SE|EX)", "MAVERICK $1");
关于java - 正则表达式 : Insert space in the string after a matched pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31494413/