我必须替换成千上万个包含“ SomeWord-”的字符串,后跟四个数字(可以是任何四个数字)。
例如,文件可以包含:
@Component
public class EntityExposingIdConfiguration extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
try {
Field exposeIdsFor = RepositoryRestConfiguration.class.getDeclaredField("exposeIdsFor");
exposeIdsFor.setAccessible(true);
ReflectionUtils.setField(exposeIdsFor, config, new ListAlwaysContains());
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
class ListAlwaysContains extends ArrayList {
@Override
public boolean contains(Object o) {
return true;
}
}
}
它需要变成另一个字符串:
blah:"someValue1",
otherThing:"someOtherValue1",
importantThing:"SomeWord-1232",
etc:".....",
importantThing:"SomeWord-4567",
otherThing:"SomeWord-8438"
使用sed,grep,vim等最干净的方法是什么?
答案 0 :(得分:3)
在Vim中可以做到
:%s/SomeWord-\d\+/SomeOtherWord-ABC/g
与1个或多个数字匹配的数字,如果您希望恰好四个数字,则可以这样做
:%s/SomeWord-\d\{4}/SomeOtherWord-ABC/g