001 /*--------------------------------------------------------------------------+ 002 $Id: RegexReplacement.java 26283 2010-02-18 11:18:57Z juergens $ 003 | | 004 | Copyright 2005-2010 Technische Universitaet Muenchen | 005 | | 006 | Licensed under the Apache License, Version 2.0 (the "License"); | 007 | you may not use this file except in compliance with the License. | 008 | You may obtain a copy of the License at | 009 | | 010 | http://www.apache.org/licenses/LICENSE-2.0 | 011 | | 012 | Unless required by applicable law or agreed to in writing, software | 013 | distributed under the License is distributed on an "AS IS" BASIS, | 014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 015 | See the License for the specific language governing permissions and | 016 | limitations under the License. | 017 +--------------------------------------------------------------------------*/ 018 package edu.tum.cs.commons.string; 019 020 import java.util.regex.Pattern; 021 import java.util.regex.PatternSyntaxException; 022 023 /** 024 * Default implementation of {@link IRegexReplacement}. 025 * 026 * 027 * @author Florian Deissenboeck 028 * @author $Author: juergens $ 029 * @version $Rev: 26283 $ 030 * @levd.rating GREEN Hash: 09EE4C50B63A33A746B17374B444B592 031 */ 032 public class RegexReplacement implements IRegexReplacement { 033 034 /** The pattern. */ 035 private final Pattern pattern; 036 037 /** The replacement. */ 038 private final String replacement; 039 040 /** 041 * Create a new regex replacement. Syntax for patterns and replacements is 042 * specified in the API documentation of {@link java.util.regex.Pattern} and 043 * {@link java.util.regex.Matcher}. 044 * 045 * @throws PatternSyntaxException 046 * if the pattern has a syntax error 047 */ 048 public RegexReplacement(String regex, String replacement) 049 throws PatternSyntaxException { 050 pattern = Pattern.compile(regex); 051 this.replacement = replacement; 052 } 053 054 /** 055 * Create a new regex replacement that does not replace the pattern matches 056 * by another string but deletes them. Syntax for patterns is specified in 057 * the API documentation of {@link java.util.regex.Pattern}. 058 * 059 * @throws PatternSyntaxException 060 * if the pattern has a syntax error 061 */ 062 public RegexReplacement(String regex) throws PatternSyntaxException { 063 this(regex, StringUtils.EMPTY_STRING); 064 } 065 066 /** {@inheritDoc} */ 067 public Pattern getPattern() { 068 return pattern; 069 } 070 071 /** {@inheritDoc} */ 072 public String getReplacement() { 073 return replacement; 074 } 075 076 }