解析并删除java正则表达式中的特殊字符 [英] Parse and remove special characters in java regex

查看:225
本文介绍了解析并删除java正则表达式中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我们正在查看其他一些正则表达式帖子,我们在一个实例中删除特殊情况时遇到了问题;特殊字符位于单词的开头。

So we were looking at some of the other regex posts and we are having trouble removing a special case in one instance; the special character is in the beginning of the word.

我们的代码中有以下行:

We have the following line in our code:

String k = s.replaceAll("([a-z]+)[()?:!.,;]*", "$1");

其中s是单数字。例如,当解析句子(hi hi hi)时,通过对其进行标记,然后对每个标记执行replaceAll函数,我们得到一个输出:

where s is a singular word. For example, when parsing the sentence "(hi hi hi)" by tokenizing it, and then performing the replaceAll function on each token, we get an output of:

(hi
hi
hi

什么我们在正则表达式中是否缺少?

What are we missing in our regex?

推荐答案

您可以使用更简单的方法 - 替换您执行的字符 not 想要空格:

You can use an easier approach - replace the characters that you do not want with spaces:

String k = s.replaceAll("[()?:!.,;]+", " ");

这篇关于解析并删除java正则表达式中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆