参考资料:
简单批量替换
举例:将and
批量替换为&&
Python实现
import re
def transformSimple(fromRegex, toText, inText):
return re.sub(fromRegex, toText,inText, flags =re.I)
if __name__ == "__main__":
inText = "x =1 and y =2"
fromRegex = " and "
toText = " && "
outText = transformSimple(fromRegex,toText,inText )
print(outText)
## OUTPUT: x =1 && y =2
Java实现
import java.util.*;
import java.util.regex.*;
public class RegexTest {
private static String transformSimple(String regexPattern, String replText, String inText)