在Java中为.split使用多个分隔符 [英] Using multiple delimiters for .split in Java

查看:207
本文介绍了在Java中为.split使用多个分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的代码只用空格分隔单词,但我也想用'。'和,分隔。这是我目前的代码:

Right now my code only separates words by white space, but I also want to separate by '.' and "," too. Here is my current code:

for (String words : input.split("\\s+"))

例如,如果用户输入面包,牛奶,鸡蛋或嗯... awkss它会考虑一个单词,我希望每个单词都是它自己的单词。

For example, if the user entered "bread,milk,eggs" or "Um...awkss" It would consider that one word, and I want each word to be it's own word.

当我在这里时,我无法得到

And while I'm here, I can't get

input.isAlpha() 

工作。

推荐答案

你可以使用这个正则表达式进行拆分

You can split using this regex

input.split("\\s+|.+|,+")

或简单地说:

input.split("[\\s.,]+")

请记住点不必在方括号内转义

这篇关于在Java中为.split使用多个分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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