在正则表达式中替换1ST,(1ST),2ND,(2ND)等

时间:2015-03-10 06:27:06

标签: java regex

我有一个名字,如1ST KING NAME或2ND Queen和(3RD)KING。我想用正则表达式从我的名字替换所有1ST,(1ST),2ND,(2ND),3RD,(3RD),4TH,(4TH)等。我能够独立完成,但我想要一个正则表达式。

1 个答案:

答案 0 :(得分:1)

使用string.replaceAll功能。这会将1ST2ND3RD4TH替换为空字符串。

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

DEMO