使用正则表达式将字符串拆分为android中的多个字符串

时间:2015-04-14 05:01:59

标签: java android regex string split

我正在研究一个android项目做相关的解析.suppose我想把字符串分成自定义格式

我的字符串

Power supply will be suspended in the following areas on 13-04-15 between 9.00 A.M. to 2.00 P.M. for maintenance work. NUNGAMBAKKAM AREA : Nungambakkam High road(Door no. 33 to 66), Ponnankipuram, Avenue road, New street. TI CYCLE AREA : Cholapuram Main road, Complete Thiruvenkata nagar, Stedford Hospital, Lenin nagar, Teachers colony, Sivananda nagar, Municipal Court and CTH road, Post office, Railway station road, School area, Ramapuram, South park st, Ambattur market, Thasildhar office

所以我想把字符串分成这样的

Power supply will be suspended in the following areas on 13-04-15 between 9.00 A.M. to 2.00 P.M. for maintenance work
NUNGAMBAKKAM AREA
Nungambakkam High road(Door no. 33 to 66)
Ponnankipuram
Avenue road
...........
Thasildhar office

那我怎么能得到这个android.help我来获取值

3 个答案:

答案 0 :(得分:3)

(?<=[a-z])\.(?=\s*[A-Z])|[,:]

你可以尝试一下。参见演示。

https://regex101.com/r/sJ9gM7/88

对于java,它将是

(?<=[a-z])\\.(?=\\s*[A-Z])|[,:]

答案 1 :(得分:0)

你要做的事情非常困难,除非涉及大量硬编码或重新考虑如何存储字符串。计算机不像人脑一样工作,所以你只能告诉他们你希望它分成哪个字符或者哪个字符索引。 我建议你使用一些String来表示意图断线,比如<LF>(换行),然后你可以使用String内置方法:

String array[] = string.split("<LF>");

答案 2 :(得分:0)

您可以从 “,” 中拆分(这很容易实现。网上有很多教程可供使用。)

请先尝试自己实施,如果您在实施中遇到问题,请发布问题。