用于模式匹配和替换的正则表达式

时间:2014-10-09 07:31:59

标签: regex

我在文件中有几个具有此模式的路径

/directory/dir/A/
/directory/dir/B/
/directory/dir/C/
/directory/dir/D/

我想要一个正则表达式模式用

替换它们
/directory/dir/new-path-A/
/directory/dir/new-path-B/
/directory/dir/new-path-C/
/directory/dir/new-path-D/

由于

1 个答案:

答案 0 :(得分:0)

搜索模式:

(/dir/)[^/]*

的更换:

$1new-path-

Java代码:

String repl = input.replacAll("(/dir/)[^/]*", "$1new-path-");

RegEx Demo