public class RegExpSubstitution extends Object implements Serializable
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 "$"
这个类的一个实例可用来多次对不同的字符串进行替换,每次替换时的性能开销很小。因此它适合用在替换条件固定, 但待替换字符串数量巨大的情形下。 注意这里的正则表达式不支持“^”和“$”。
Modifier and Type | Field and Description |
---|---|
protected static boolean |
moreSpaceForSpeed |
protected String |
replacement |
protected dk.brics.automaton.RunAutomaton |
runAutomation |
Constructor and Description |
---|
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.
构造方法,用于只有一个正则表达式的情况。 |
Modifier and Type | Method and Description |
---|---|
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.
替换掉最后一个出现的地方 |
protected dk.brics.automaton.RunAutomaton runAutomation
protected String replacement
protected static final boolean moreSpaceForSpeed
public RegExpSubstitution(String replaceStr, String regExp)
replaceStr
- The string to be replaced withregExp
- Part of the string that matches with this regular expression will be replaced.public RegExpSubstitution(String replaceStr, String... regExps)
replaceStr
- The string to be replaced withregExps
- Part of the string that matches with any of these regular expressions will be replaced.public RegExpSubstitution(String replaceStr, Collection<String> regExps)
replaceStr
- The string to be replaced withregExps
- Part of the string that matches with any of these regular expressions will be replaced.protected String replace(CharSequence text, boolean firstOnly)
text
- The original String.firstOnly
- If true, then only do replacement upon the first occurrence;
otherwise, replace in all the occurrences.public String replaceFirst(CharSequence text)
text
- The original String.public String replaceAll(CharSequence text)
text
- The original String.public String replaceLast(CharSequence text)
text
- The original String.Copyright © 2014. All Rights Reserved.