标签: java regex
我有一个名字,如1ST KING NAME或2ND Queen和(3RD)KING。我想用正则表达式从我的名字替换所有1ST,(1ST),2ND,(2ND),3RD,(3RD),4TH,(4TH)等。我能够独立完成,但我想要一个正则表达式。
答案 0 :(得分:1)
使用string.replaceAll功能。这会将1ST,2ND,3RD,4TH替换为空字符串。
string.replaceAll
1ST
2ND
3RD
4TH
string.replaceAll("\\((?:1ST|2ND|3RD|4TH)\\)|\\b(?:1ST|2ND|3RD|4TH)\\b", "");
DEMO
或强>
\((?:1ST|2ND|3RD|4TH|(?:[4-9]|[1-9]\d+)TH)\)|\b(?:1ST|2ND|3RD|(?:[4-9]|[1-9]\d+)TH)\b