问题1.
我正在尝试将我的字符串从"(a)更改为(b)该行是正确的"至"(a)2(b)该线是正确的"。为此我使用此代码,但它不能满足我的要求
strtargetsentence=strtargetsentence_.replaceAll"([\(]([A-Za-z0-9])[\)][ ](to|and|or)[ ][\(]([A-Za-z0-9])[\)])","([\(]([A-Za-z0-9])[\)][ ](2|&|aur)[ ][\(]([A-Za-z0-9])[\)])"
同样我也想做这些组合
(a) to (b)--->(a) 2 (b)
(b) to (e)--->(b) & (e)
(1) to (2)--->(1) aur (2)
问题2.
Silimarly对于这些组合我也尝试使用以下代码
[a] we are going ---->[a] we are going
1)we are going---->1)we are going
strtargetsentence=strtargetsentence_.replaceAll("([\\[|\\(])?([0-9A-Za-z]+)([\\.|\\)|\\]?])", "$1$2$3");
答案 0 :(得分:2)
我认为你应该写几个正则表达式替换。对于问题1,可能导致三个替换语句如下:
String strtargetsentence_ = "(a) to (b)";
String pattern1 = "(\\(\\w\\)) to (\\(\\w\\))";
String pattern2 = "(\\(\\w\\)) and (\\(\\w\\))";
String pattern3 = "(\\(\\w\\)) or (\\(\\w\\))";
String strtargetsentence=strtargetsentence_.replaceAll(pattern1,"$1 2 $2")
.replaceAll(pattern2,"$1 & $2")
.replaceAll(pattern3,"$1 aur $2");
System.out.println(strtargetsentence);
第二个问题的替代可能如下:
String strtargetsentence=strtargetsentence_.replaceAll("(\\[?\\w)(\\]|\\))( ?.*)", "$1$2$3");