简体   繁体   English

Java正则表达式用于$替换

[英]Java regex for $ replacement

Question 1. 问题1
I am trying to change my string from "(a) to (b) the line is correct" to "(a) 2 (b) the line is correct". 我正在尝试将我的字符串从“(a)更改为(b)正确的行”更改为“(a)2(b)正确的行”。 for that i am using this code but it not working as my requirement 为此,我正在使用此代码,但它不能按我的要求工作

strtargetsentence=strtargetsentence_.replaceAll"([\(]([A-Za-z0-9])[\)][ ](to|and|or)[ ][\(]([A-Za-z0-9])[\)])","([\(]([A-Za-z0-9])[\)][ ](2|&|aur)[ ][\(]([A-Za-z0-9])[\)])"

Similarly i also want to do these combination 同样,我也想做这些组合

(a) to (b)--->(a) 2 (b)
(b) to (e)--->(b) & (e)  
(1) to (2)--->(1) aur (2)

Question 2. 问题2。
Silimarly for these combination also i am trying this with following code 对于这些组合,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");

I think you should write several regex replacements. 我认为您应该写几个正则表达式替换。 For question 1 that could result in three replace statements like these: 对于问题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);

A replacement for your second question could be the following: 您的第二个问题的替代内容可能是:

String strtargetsentence=strtargetsentence_.replaceAll("(\\[?\\w)(\\]|\\))( ?.*)", "$1$2$3");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM