I want to convert a software version number into a github tag name by regular expression.
For example, the version of ognl
is usually 3.2.1
. What I want is the tag name OGNL_3_2_1
So we can use String::replaceAll(String regex, String replacement)
method like this
"3.2.1".replaceAll("(d+).(d+).(d+)", "OGNL_$1_$2_$3")
And we can get the tag name OGNL_3_2_1
easily.
But when it comes to 3.2
, I want the regex still working so I change it into (d+).(d+)(?:.(d+))?
.
Execute the code again, what I get is OGNL_3_2_
rather than OGNL_3_2
. The underline _
at the tail is not what I want. It is resulted by the null group for $3
So how can I write a suitable replacement to solve this case?
When the group for $3 is null, the underline _
should disappear
Thanks for your help !!!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…