【发布时间】:2016-07-05 07:41:06
【问题描述】:
我有一个像这样的字符串“abcd !@&$%^^&*()!/”。我有一个单独字符串中字符的所有实体代码的列表,即只编码另一个字符串中的那些字符“!=&4 ....^ = 9 ...”。我想通过正则表达式将所有特殊字符转换为它们的实体,除了字母数字,因为对字符使用循环太慢了。
例如它应该显示“abc ..;..”,换句话说,将键盘上的所有特殊字符转换成单词。
我可以写一个有效的正则表达式吗?我已经用循环尝试过这个,但是一个一个地查看每个字符并维护其他字符串中所有特殊字符实体的列表太慢了
有库,但它们不会转换所有字符。
我写的代码
// String to be encoded
String sDecoded = "abcd !@#$%^&*();'m,";
// Special character entity list to put instead to special character. It is tokenized on cross and divide symbol as it cannot be entered by user on keyboard
String specialCharacters = "&÷$amp;×–÷–"
// Check the input
if (sDecoded == null || sDecoded.trim ().length () == 0)
return (sDecoded);
// Use StringTokenizer which is faster than split method
StringTokenizer st = new StringTokenizer(specialCharacters, "×");
String[] reg = null;
String[] charactersArray = sDecoded.split("");
String sEncoded = "";
// now loop on it and in each iteration, we will be getting a decodedCharacter:EncodedEntity pair
for(int i = 0; i < charactersArray.length; i++)
{
st = new StringTokenizer(specialCharacters, "×");
while(st.hasMoreElements())
{
reg = st.nextElement().toString().split("÷");
// This is an error, the character should not be blank ever because it will be character that we will encode
if(StringUtils.isBlank(reg[0]))
return sDecoded;
String c = charactersArray[i];
if(c.equalsIgnoreCase(reg[0]))
{
sEncoded = sEncoded + c.replace(reg[0], reg[1]);
break;
}
if(st.countTokens() == 0)
sEncoded = sEncoded + c.toString();
}
}
return (sEncoded);
【问题讨论】:
-
如果逐个字符执行此操作很慢,则使用正则表达式会更慢。正则表达式不是魔法棒——当它们不匹配时它会扫描字符串和回溯。如何准确地展示您的尝试,我们也许可以帮助您改进它。
-
您粘贴的代码无法编译 - 字符串文字有问题。
-
为什么要使用复杂的
specialCharacters字符串?为什么不使用正确的Map,这样搜索起来会更快? -
其实我不会用String来装。这只是一个例子。这些字符将保存在配置文件中的代码之外,我们可以在不更改代码的情况下进行更改。因此,无论我们要添加什么字符,我们都以标记化字符串的方式添加到该 .ini 文件中并从那里读取。
-
太好了,我正在询问它将在 Java 程序中采用的形式。毕竟,您不会考虑在程序中一次又一次地读取
ini文件——您将一次将其加载到数据结构中。该数据结构不应是您创建的复杂字符串,而是一个映射。