原创作者: site120
阅读:4537次
评论:0条
更新时间:2011-05-26
xml 代码
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse - Hibernate Tools
- -->
- <hibernate-mapping>
- <class name="cn.site120.TestRegex" table="XXX_123456_ver" dynamic-update="true" dynamic-insert="true">
- <id name="id" type="java.lang.Long">
- <column name="id" />
- <generator class="native" />
- </id>
- <property name="name" type="java.lang.String">
- <column name="name" length="50" />
- </property>
- <property name="versionNumber" type="java.lang.Integer">
- <column name="versionNumber" not-null="true" />
- </property>
- </class>
- </hibernate-mapping>
现在想用程序动态修改table="XXX_123456_ver"这里面的123456替换为其它代码,比如说022,010,等.
可以构造一个正则表达式来处理,由于这里面的123456也可能处于其它位置,比如: XXX_XXX_123456,所以可以这么来写
java 代码
- String regex = "(?<=_)([0-9]{3,4})(?=[_]?)";
- File tmpdir = new File(System.getProperty("java.io.tmpdir"));
- String classpath = TestNull.class.getClassLoader().getResource("").toString();
- classpath = classpath.substring("file:/".length());
- File configFile = new File(classpath + "config/XXX.hbm.xml");
- BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile)));
- PrintWriter writer = new PrintWriter(tmpdir.getPath() + "/" + "XXX.hbm.xml");
- StringBuffer content = new StringBuffer();
- String temp = "";
- while ((temp=reader.readLine()) != null)
- {
- content.append(temp);
- content.append("\r\n");
- }
- StringBuffer content_new = new StringBuffer();
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(content);
- if (matcher.find())
- {
- matcher.appendReplacement(content_new , "123456");
- }
- matcher.appendTail(content_new);
- //... 再将内容写到特定的目录下去. ...
这里面(?<=_)([0-9]{3,4})(?=[_]?)中的(?<=)为反向预搜索,表示在该缝隙的左边有一个"_"符号,且不参于匹配
(?=)表示正向预搜索,表示在该缝隙的右边可能有一个"_"符号,且不参于匹配
评论 共 0 条 请登录后发表评论