net.sf.jabb.util.text
Class RegExpSubstitution

java.lang.Object
  extended by net.sf.jabb.util.text.RegExpSubstitution
All Implemented Interfaces:
Serializable

public class RegExpSubstitution
extends Object
implements Serializable

The utility to substitute part of the String that matches specified regular expression(s) with another specified String.
一个工具类,它可以对一个或多个正则表达式所匹配到的字符串中的内容作替换。

An instance of this class can be used many times for substitution for different Strings. The performance overhead of each time of substitution is very small. Therefore it is very suitble to be used in occasions that the substitution criteria is fixed while the Strings to be substituted are of huge volume. Please be aware that regular expressions here do not support "^" and "$"

这个类的一个实例可用来多次对不同的字符串进行替换,每次替换时的性能开销很小。因此它适合用在替换条件固定, 但待替换字符串数量巨大的情形下。 注意这里的正则表达式不支持“^”和“$”。

Author:
Zhengmao HU (James)
See Also:
Serialized Form

Field Summary
protected static boolean moreSpaceForSpeed
           
protected  String replacement
           
protected  dk.brics.automaton.RunAutomaton runAutomation
           
 
Constructor Summary
RegExpSubstitution(String replaceStr, Collection<String> regExps)
          Constructor with several regular expressions.
构造方法,用于有多个正则表达式的情况。
RegExpSubstitution(String replaceStr, String... regExps)
          Constructor with several regular expressions.
构造方法,用于有多个正则表达式的情况。
RegExpSubstitution(String replaceStr, String regExp)
          Constructor with one regular expression.
构造方法,用于只有一个正则表达式的情况。
 
Method Summary
protected  String replace(CharSequence text, boolean firstOnly)
          Do replacement.
进行替换。
 String replaceAll(CharSequence text)
          Replace all the occurrences.
替换所有出现的地方
 String replaceFirst(CharSequence text)
          Replace only the first occurrence.
只替换第一个出现的地方
 String replaceLast(CharSequence text)
          Replace only the last occurrence.
替换掉最后一个出现的地方
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

runAutomation

protected dk.brics.automaton.RunAutomaton runAutomation

replacement

protected String replacement

moreSpaceForSpeed

protected static final boolean moreSpaceForSpeed
See Also:
Constant Field Values
Constructor Detail

RegExpSubstitution

public RegExpSubstitution(String replaceStr,
                          String regExp)
Constructor with one regular expression.
构造方法,用于只有一个正则表达式的情况。

Parameters:
replaceStr - The string to be replaced with
替换为这个字符串
regExp - Part of the string that matches with this regular expression will be replaced.
匹配到这个正则表达式的部分会被替换

RegExpSubstitution

public RegExpSubstitution(String replaceStr,
                          String... regExps)
Constructor with several regular expressions.
构造方法,用于有多个正则表达式的情况。

Parameters:
replaceStr - The string to be replaced with
替换为这个字符串
regExps - Part of the string that matches with any of these regular expressions will be replaced.
匹配到这些正则表达式中的任意一个的部分都会被替换

RegExpSubstitution

public RegExpSubstitution(String replaceStr,
                          Collection<String> regExps)
Constructor with several regular expressions.
构造方法,用于有多个正则表达式的情况。

Parameters:
replaceStr - The string to be replaced with
替换为这个字符串
regExps - Part of the string that matches with any of these regular expressions will be replaced.
匹配到这些正则表达式中的任意一个的部分都会被替换
Method Detail

replace

protected String replace(CharSequence text,
                         boolean firstOnly)
Do replacement.
进行替换。

Parameters:
text - The original String.
替换前的原始字符串。
firstOnly - If true, then only do replacement upon the first occurrence; otherwise, replace in all the occurrences.
如果为true则只替换第一个出现的地方,否则全部替换。
Returns:
The String after substitution; If no match found, the same String as the input one will be returned.
替换后的结果;如果没有发生替换,则结果与原来输入的字符串完全相同。

replaceFirst

public String replaceFirst(CharSequence text)
Replace only the first occurrence.
只替换第一个出现的地方

Parameters:
text - The original String.
替换前的原始字符串。
Returns:
The String after substitution; If no match found, the same String as the input one will be returned.
替换后的结果;如果没有发生替换,则结果与原来输入的字符串完全相同。

replaceAll

public String replaceAll(CharSequence text)
Replace all the occurrences.
替换所有出现的地方

Parameters:
text - The original String.
替换前的原始字符串。
Returns:
The String after substitution; If no match found, the same String as the input one will be returned.
替换后的结果;如果没有发生替换,则结果与原来输入的字符串完全相同。

replaceLast

public String replaceLast(CharSequence text)
Replace only the last occurrence.
替换掉最后一个出现的地方

Parameters:
text - The original String.
替换前的原始字符串。
Returns:
The String after substitution; If no match found, the same String as the input one will be returned.
替换后的结果;如果没有发生替换,则结果与原来输入的字符串完全相同。


Copyright © 2012. All Rights Reserved.