public class ReplaceEvaluatorReplaceWithImage implements ReplaceEvaluator {in public placesynchronized int

Aspose.Words使用教程之在文档中找到并替换文本
Aspose.Words
是一款先进的文档处理控件,在不使用Microsoft Words的情况下,它可以使用户在各个应用程序中执行各种文档处理任务,其中包括文档的生成、修改、渲染、打印,文档格式转换和邮件合并等文档处理。此外,Aspose.Words支持DOC,OOXML,RTF,HTML,OpenDocument, PDF, XPS, EPUB和其他格式。
使用范围:在前的范围内替换查找或替换特定的字符串,因为它会返回替换的数量,所以它是在没有替换的条件下搜索字符串是非常有用的。如果一个捕获或替换包含一个或多个特殊字符:段落,细胞破裂,部分断裂,现场开始,字段分隔符、字段,内联图片,绘画对象,脚注的字符串时,会出现异常时。
在一定的范围内,替代方法提供了几个过载。以下是他们提供的可能性:
您可以指定一个字符串被替换,一旦被替换,所有这个字符串都将被替换,替换是否区分大小写,或者只有独立的单词才会受到影响。注意一个词被定义为仅由字母数字字符组成。如果只执行替换匹配的整个单词,输入字符串包含了符号,那么这个单词不会被搜索到。
您可以通过一个正则表达式模式用于查找匹配和一个字符串,然后替换它们。这种过载替换通过正则表达式捕捉到整个匹配。
你可以通过正则表达式模式和一个对象实现[{{ IReplacingCallback }}]接口。这提出了一个用户定义的方法,它在每个步骤中评估替换,您也可以指示更换是应该向前还是向后的方向。建议如果在更换过程中要删除节点那么更换应该向后执行,以避免在更换过程中删除节点有任何可能出现的潜在问题。一个实现回调接口的类将定义一个[IReplacingCallback.Replacing]方法,它接受提供定制的替换数据操作[{ { ReplacingArgs } }]对象。这个方法应该返回一个[{ { ReplaceAction } }]枚举值,指定当前匹配更换操作期间发生了什么——它是否应该更换,跳过,或整个替换操作应该终止。
下面的例子展示如何使用前面提到的过载。样例类提供的使用了Range.Replace 方法:
示例1 将所有出现的“sad”替换为“bad”。
示例2 将所有出现的“sad”或者“mad”替换成“bad”。
示例3 使用替换评估方法来连接出现的单词“sad”或“bad”,每出现一次计数值相应增加。
Example 1:用一个词换另一个词
将所有出现的“sad”替换为“bad”。
Document doc = new Document(MyDir + @&in.docx&);doc.Range.Replace(&sad&, &bad&, false, true);
Visual Basic
Dim doc As New Document(MyDir & &Document.doc&)doc.Range.Replace(&sad&, &bad&, False, True)
Example 2:用一个词替换两个相近的词
使用“bad”替换所有“sad”和“mad”。
Document doc = new Document(MyDir + &Document.doc&);doc.Range.Replace(new Regex(&[s|m]ad&), &bad&);
Visual Basic
Dim doc As New Document(MyDir & &Document.doc&)doc.Range.Replace(New Regex(&[s|m]ad&), &bad&)
Example 3:使用一个自定义计数器
如何替换为一个自定义计数器
public void ReplaceWithEvaluator(){Document doc = new Document(MyDir + &Range.ReplaceWithEvaluator.doc&);doc.Range.Replace(new Regex(&[s|m]ad&), new MyReplaceEvaluator(), true);doc.Save(MyDir + &Range.ReplaceWithEvaluator Out.doc&);}private class MyReplaceEvaluator : IReplacingCallback{/// &summary&/// This is called during a replace operation each time a match is found./// This method appends a number to the match string and returns it as a replacement string./// &/summary&ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e){e.Replacement = e.Match.ToString() + mMatchNumber.ToString();mMatchNumber++;return ReplaceAction.R}private int mMatchN}
Visual Basic
Public Sub ReplaceWithEvaluator()Dim doc As New Document(MyDir & &Range.ReplaceWithEvaluator.doc&)doc.Range.Replace(New Regex(&[s|m]ad&), New MyReplaceEvaluator(), True)doc.Save(MyDir & &Range.ReplaceWithEvaluator Out.doc&)End SubPrivate Class MyReplaceEvaluatorImplements IReplacingCallback''' &summary&''' This is called during a replace operation each time a match is found.''' This method appends a number to the match string and returns it as a replacement string.Private Function IReplacingCallback_Replacing(ByVal e As ReplacingArgs) As ReplaceAction Implements IReplacingCallback.Replacinge.Replacement = e.Match.ToString() & mMatchNumber.ToString()mMatchNumber += 1Return ReplaceAction.ReplaceEnd FunctionPrivate mMatchNumber As IntegerEnd Class
立即下载Aspose.Words最新版
最新教程周点击榜
微信扫一扫Evaluator (JEval)
net.sourceforge.jeval
Class Evaluator
java.lang.Object
net.sourceforge.jeval.Evaluator
public class Evaluatorextends java.lang.Object
This class is used to evaluate mathematical, string, Boolean and functional
expressions. It is the main entry point into the JEval API.
The following types of expressions are supported:
mathematical Expression involving numbers. Numbers are treated
as doubles, so resulting numbers will contain at least one decimal place.
string String can also be added together, compared, etc...
Boolean Expression that evaluate to true (1.0) and false (0.0).
functional Custom functions can be created or there are many
Math and String functions that JEval supplies with this class.
The following operators are supported:
( open parentheses
) closed parentheses
+ addition (for numbers and strings)
- subtration
* mutliplication
/ division
+ unary plus
- unary minus
= equal (for numbers and strings)
!= not equal (for numbers and strings)
< less than (for numbers and strings)
<= less than or equal (for numbers and strings)
> greater than (for numbers and strings)
>= greater than or equal (for numbers and strings)
&& boolean and
|| boolean or
! boolean not
Allows for prebuilt and custom functions.
JEval already comes with many functions which represent most of the
methods in the Math and String classes in the standard JDK.
Thirty-nine math and string functions come built in. See the
net.sourceforge.jeval.functions.math and
net.sourceforge.jeval.functions.string packages for details on these ready to
use functions. You can choose to not load these functions if we you want to
gain a small improvement in performance.
Functions must be followed by an open parentheses and a closed
parentheses which contain any required parameters.
For more details on functions, see the Function class and the test
Allows for variables.
Variable must be enclosed by a pound sign and open brace #{ and a closed
brace }. i.e. expression = "#{a} + #{b}"
Two math variables come built in. The E and PI variables represent the
same value as the Math.E and Math.PI constants in the standard Java SDK. You
can choose not to load these variables.
Notes on expression parsing:
Spaces are ignored when parsing expressions.
The order of precedence used by this class is as follows from highest to
The expression is evaluated as one or more subexpressions.
Subexpressions within open parentheses and closed parentheses are evaluated
before other parts of the expression.
Inner most subexpression are evaluated first working outward.
Subexpressions at the same level are evaluated from left to right.
When evaluating expressions and subexpressions, operators are evaluated
with the following precedence listed below.
Operators with with the same precedence are evaluated from left to
Once the expression is parsed, Variables are replaced with their values.
The evaluator has its own internal variable map that it used to resolve
variable values. All of the variable related methods on the evaluator refer
to this internal map. You can choose to set you own variable resolver on your
evaluator instance. IF you do this, then variables resolved by your resolver
will override any variables in the evaluator's internal variable map.
Functions are then executed and replaced with their results. Function
arguments are each inidividually evaluated as subexpressions that are comma
separated. This gives you the ability to use nested functions in your
expressions. You can choose not to evaluate function arguments as expressions
and instead let the functions handle the arguments themselves. This in effect
turns off nested expressions, unless you code nexted expression support into
yours custom functions.
Once all variables and functions are resolved, then the parsed
expression and subexpressions are evaluated according to operator precedence.
Operator precedence:
+ unary plus, - unary minus, ! boolean not
* multiplication, / division, % modulus
+ addition, - subtraction
< less than,
greater than, >= greater than or
= equal, != not equal
&& boolean and
|| boolean or
Function and variable names can not break any of the following rules:
can not start with a number
can not contain an operator (see the above list of operators)/li>
can not contain a quote character - single or double/li>
can not contain a brace character - open or closed/li>
can not contain one of the following special characters: #, ~ , ^ !
Other Notes:
This class is not thread safe.
Allows for the quote character (single or double) to be specified at run
time. Quote characters are required for specifying string values.
Expressions can contain different types of expressions within the same
expression. However, Numeric and string types can not be mixed in a left /
right oeprand pair.
An expression can be parsed before being evaluated by calling the parse()
method. This may save on response time if parsing takes more than a few
seconds. However, parsing is usally very fast, so this is probably not
Also, if an expression does not change, it will not be parsed each
time the expression is evaluated. Therefore, variables values can change and
the expression can be evaluataed again without having to re-parse the
expression.
Nested functions calls are supported. Nested function support can be
turned off to improve performance. Custom functions can be coded to handle
nested calls instead if desired.
The string used to start variables, "#{", can not appear in an
expression.
See the evaluate methods in this class, JUnit tests and samples for more
&&&&&&&&&&The default constructor.
(char&quoteCharacter,
boolean&loadMathVariables,
boolean&loadMathFunctions,
boolean&loadStringFunctions,
boolean&processNestedFunctions)
&&&&&&&&&&The main constructor for Evaluator.
&&&&&&&&&&Removes all of the functions at one time.
&&&&&&&&&&Removes all of the variables at one time.
&java.lang.String
&&&&&&&&&&This method evaluates mathematical, boolean or functional expressions.
&java.lang.String
(boolean&keepQuotes,
boolean&wrapStringFunctionResults)
&&&&&&&&&&This method evaluates mathematical, boolean or functional expressions.
&java.lang.String
(java.lang.String&expression)
&&&&&&&&&&This method evaluates mathematical, boolean or functional expressions.
&java.lang.String
(java.lang.String&expression,
boolean&keepQuotes,
boolean&wrapStringFunctionResults)
&&&&&&&&&&This method evaluates mathematical, boolean or functional expressions.
(java.lang.String&expression)
&&&&&&&&&&This method is a simple wrapper around the evaluate(String) method.
(java.lang.String&functionName)
&&&&&&&&&&Returns a funtion from the list of functions.
&java.util.Map
&&&&&&&&&&Rturns the map of functions currently set on this object.
(java.lang.String&expression)
&&&&&&&&&&This method is a simple wrapper around the evaluate(String) method.
&&&&&&&&&&Returns the current quote character in use.
&&&&&&&&&&Returns the variable resolver.
&java.util.Map
&&&&&&&&&&Rturns the map of variables currently set on this object.
&java.lang.String
(java.lang.String&variableName)
&&&&&&&&&&Returns the value for a variable in the list of variables.
protected &boolean
(java.lang.String&expressionString)
&&&&&&&&&&Determines if the string represents a valid expression string or not.
(java.lang.String&name)
&&&&&&&&&&This method verifies if a function or variable name is valid or not.
(java.lang.String&expression)
&&&&&&&&&&This method parses a mathematical, boolean or functional expressions.
(&function)
&&&&&&&&&&Adds a function to the list of functions to use when evaluating
expressions.
(java.lang.String&variableName,
java.lang.String&variableValue)
&&&&&&&&&&Adds or replaces a variable to the list of variables to use when
evaluating expressions.
(java.lang.String&functionName)
&&&&&&&&&&Removes the function from the list of functions to use when evaluating
expressions.
(java.lang.String&variableName)
&&&&&&&&&&Removes the variable from the list of variables to use when evaluating
expressions.
&java.lang.String
(java.lang.String&expression)
&&&&&&&&&&Replaces the variables in the expression with the values of the variables
for this instance of the evaluator.
(java.util.Map&functions)
&&&&&&&&&&Sets the map of functions for this object.
(char&quoteCharacter)
&&&&&&&&&&Sets the quote character to use when evaluating expressions.
(&variableResolver)
&&&&&&&&&&Sets the variable resolver for this class.
(java.util.Map&variables)
&&&&&&&&&&Sets the map of variables for this object.
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
public Evaluator()
The default constructor. This constructor calls the five parameter
Evaluator constructor and passes in the following default values:
SINGLE_QUOTE, true, true, true and true.
public Evaluator(char&quoteCharacter,
boolean&loadMathVariables,
boolean&loadMathFunctions,
boolean&loadStringFunctions,
boolean&processNestedFunctions)
The main constructor for Evaluator.
Parameters:quoteCharacter - The quote character to use when evaluating expression.loadMathVariables - Indicates if the standard Math variables should be loaded or
not.loadMathFunctions - Indicates if the standard Math functions should be loaded or
not.loadStringFunctions - Indicates if the standard String functions should be loaded or
not.processNestedFunctions - Indicates if nested function calls should be processed or not.
java.lang.IllegalArgumentException - Thrown when the quote character is not a valid quote
character.
getQuoteCharacter
public char getQuoteCharacter()
Returns the current quote character in use.
Returns:The quote character in use.
setQuoteCharacter
public void setQuoteCharacter(char&quoteCharacter)
Sets the quote character to use when evaluating expressions.
Parameters:quoteCharacter - The quote character to use when evaluating expressions.
java.lang.IllegalArgumentException - Thrown when the quote character is not a valid quote
character.
putFunction
public void putFunction(&function)
Adds a function to the list of functions to use when evaluating
expressions.
Parameters:function - The function being added.
java.lang.IllegalArgumentException - Thrown when the function name is not valid or the function
name is already in use.
getFunction
getFunction(java.lang.String&functionName)
Returns a funtion from the list of functions. If the function can not be
found in the list of functions, then null will be returned.
Parameters:functionName - The name of the function to retrieve the value for.
Returns:The value for a function in the list of function.
removeFunction
public void removeFunction(java.lang.String&functionName)
Removes the function from the list of functions to use when evaluating
expressions.
Parameters:functionName - The name of the function to remove.
clearFunctions
public void clearFunctions()
Removes all of the functions at one time.
getFunctions
public java.util.Map getFunctions()
Rturns the map of functions currently set on this object.
Returns:the map of functions currently set on this object.
setFunctions
public void setFunctions(java.util.Map&functions)
Sets the map of functions for this object.
Parameters:The - map of functions for this object.
putVariable
public void putVariable(java.lang.String&variableName,
java.lang.String&variableValue)
Adds or replaces a variable to the list of variables to use when
evaluating expressions. If the variable already exists, then its value
will be overlaid.
Parameters:variableName - The name of the variable being set.variableValue - The value for the variable being set.
getVariableValue
public java.lang.String getVariableValue(java.lang.String&variableName)
Returns the value for a variable in the list of variables. If the
variable can not be found in the list of variables, then null will be
Parameters:variableName - The name of the variable to retrieve the value for.
Returns:The value for a variable in the list of variables.
Throws - an EvaluatorException if the variable name can not be
removeVaraible
public void removeVaraible(java.lang.String&variableName)
Removes the variable from the list of variables to use when evaluating
expressions.
Parameters:variableName - The name of the variable to remove.
clearVariables
public void clearVariables()
Removes all of the variables at one time.
getVariables
public java.util.Map getVariables()
Rturns the map of variables currently set on this object.
Returns:the map of variables currently set on this object.
setVariables
public void setVariables(java.util.Map&variables)
Sets the map of variables for this object.
Parameters:variables - The map of variables for this object.
getVariableResolver
getVariableResolver()
Returns the variable resolver.
The variable resolver can be used by
the user to resolve their own variables.
Variables in the variable
resolver override any variables that are in this classes internal
variable map.
Returns:The variable resolver.
setVariableResolver
public void setVariableResolver(&variableResolver)
Sets the variable resolver for this class.
Varaibles resolved by the
variable resolver will override any variables in this class's internal
variable map.
Parameters:variableResolver - The variable resolver for this class.
public java.lang.String evaluate(java.lang.String&expression)
This method evaluates mathematical, boolean or functional expressions.
See the class description and test classes for more information on how to
write an expression. If quotes exist around a string expression, then
they will be left in the result string. Function will also have their
results wrapped with the appripriate quote characters.
Parameters:expression - The expression to evaluate.
Returns:The result of the evaluated. expression. Numbers are treated as
doubles, so resulting numbers will contain at least one decimal
EvaluateException - Thrown when an error is found while evaluating the
expression.
public java.lang.String evaluate()
This method evaluates mathematical, boolean or functional expressions.
See the class description and test classes for more information on how to
write an expression. The expression used will be the one previously
specified when using the parse method. If the parse method has not been
called before calling this method, then an exception will be thrown. If
quotes exist around a string expression, then they will be left in the
result string. Function will also have their results wrapped with the
appripriate quote characters.
Returns:The result of the evaluated. expression. Numbers are treated as
doubles, so resulting numbers will contain at least one decimal
EvaluateException - Thrown when an error is found while evaluating the
expression.
public java.lang.String evaluate(java.lang.String&expression,
boolean&keepQuotes,
boolean&wrapStringFunctionResults)
This method evaluates mathematical, boolean or functional expressions.
See the class description and test classes for more information on how to
write an expression.
Parameters:expression - The expression to evaluate.keepQuotes - Indicates if the the quotes should be kept in the result or
not. This is only for string expression that are enclosed in
quotes prior to being evaluated.wrapStringFunctionResults - Indicates if the results from functions that return strings
should be wrapped in quotes. The quote character used will be
whatever is the current quote character for this object.
Returns:The result of the evaluated expression. Numbers are treated as
doubles, so resulting numbers will contain at least one decimal
EvaluateException - Thrown when an error is found while evaluating the
expression.
public java.lang.String evaluate(boolean&keepQuotes,
boolean&wrapStringFunctionResults)
This method evaluates mathematical, boolean or functional expressions.
The expression used will be the one previously specified when using the
parse method. If the parse method has not been called before calling this
method, then an exception will be thrown. See the class description and
test classes for more information on how to write an expression.
Parameters:keepQuotes - Indicates if the the quotes should be kept in the result or
not. This is only for string expressions that are enclosed in
quotes prior to being evaluated.wrapStringFunctionResults - Indicates if the results from functions that return strings
should be wrapped in quotes. The quote character used will be
whatever is the current quote character for this object.
Returns:The result of the evaluated expression. Numbers are treated as
doubles, so resulting numbers will contain at least one decimal
EvaluateException - Thrown when an error is found while evaluating the
expression.
getBooleanResult
public boolean getBooleanResult(java.lang.String&expression)
This method is a simple wrapper around the evaluate(String) method. Its
purpose is to return a more friendly boolean return value instead of the
string "1.0" (for true) and "0.0" (for false) that is normally returned.
Parameters:expression - The expression to evaluate.
Returns:A boolean value that represents the result of the evaluated
expression.
EvaluateException - Thrown when an error is found while evaluating the
expression. It is also thrown if the result is not able to
be converted to a boolean value.
getNumberResult
public double getNumberResult(java.lang.String&expression)
This method is a simple wrapper around the evaluate(String) method. Its
purpose is to return a more friendly double return value instead of the
string number that is normally returned.
Parameters:expression - The expression to evaluate.
Returns:A double value that represents the result of the evaluated
expression.
EvaluateException - Thrown when an error is found while evaluating the
expression. It is also thrown if the result is not able to
be converted to a double value.
public void parse(java.lang.String&expression)
This method parses a mathematical, boolean or functional expressions.
When the expression is eventually evaluated, as long as the expression
has not changed, it will not have to be reparsed. See the class
description and test classes for more information on how to write an
expression.
Parameters:expression - The expression to evaluate.
EvaluateException - Thrown when an error is found while evaluating the
expression.
isExpressionString
protected boolean isExpressionString(java.lang.String&expressionString)
Determines if the string represents a valid expression string or not.
Valid expression strings must start and end with a quote character.
Parameters:expressionString - The string being evaluated.
Returns:True if the string is a valid string and false if not.
isValidName
public void isValidName(java.lang.String&name)
throws java.lang.IllegalArgumentException
This method verifies if a function or variable name is valid or not.
Function and variable names must follow these rules...
can not start with a number
can not contain an operator (see the above list of operators)
can not contain a quote character - single or double
can not contain a brace character - open or closed
can not contain one of the following special characters: #, ~ , ^ !
Parameters:name - The function or variable name being validated.
java.lang.IllegalArgumentException - Thrown if the name is invalid.
replaceVariables
public java.lang.String replaceVariables(java.lang.String&expression)
Replaces the variables in the expression with the values of the variables
for this instance of the evaluator.
Parameters:expression - The expression being processed.
Returns:A new expression with the variables replaced with their values.
EvaluateException - Thrown is an error is encoutnered while processing the
expression.
Copyright (C)
Robert Breidecker.}

我要回帖

更多关于 in public place 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信