博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分隔指定内容,提取章节数
阅读量:4550 次
发布时间:2019-06-08

本文共 1433 字,大约阅读时间需要 4 分钟。

package push;import java.util.HashMap;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;public class MatchContent {    public static void main(String[] args) {        String  split = "###第\\d+章";        String content = "###第1章aaa\n ###第4章ccc\n ###第2章bbb\n ";        Pattern p = Pattern.compile(split);        Matcher m = p.matcher(content);        int tempStart = 0;        int tempEnd = 0;        int tempChapterId = 0;        Map
chapterMap = new TreeMap
(); if(m.find()) { tempStart = m.start(); tempChapterId = Integer.valueOf(content.substring(m.start(),m.end()).replace("#", "").replace("第", "").replace("章", "")); } while(m.find()) { tempEnd=m.start(); chapterMap.put(tempChapterId, content.substring(tempStart,tempEnd)); tempStart = tempEnd; tempChapterId = Integer.valueOf(content.substring(m.start(),m.end()).replace("#", "").replace("第", "").replace("章", "")); } if(tempStart!=0) { chapterMap.put(tempChapterId, content.substring(tempStart)); } for(Integer at:chapterMap.keySet()) { System.out.println("章节数:" + at + " 内容:" + chapterMap.get(at).replace("#", "")); } }}

 

输出是

章节数:1 内容:第1章aaa

章节数:2 内容:第2章bbb
章节数:4 内容:第4章ccc

 

转载于:https://www.cnblogs.com/yanghuahui/p/3438460.html

你可能感兴趣的文章
windows创建服务
查看>>
锋利的JQuery —— JQuery性能优化
查看>>
MIT许可证
查看>>
JQuery发送Ajax请求
查看>>
SQL 中的 case when
查看>>
【DeepLearning】GoogLeNet
查看>>
【手撸一个ORM】第六步、对象表达式解析和Select表达式解析
查看>>
MsDepSvc 启动失败
查看>>
总结十四
查看>>
泛型约束
查看>>
websocket入门
查看>>
AOP技术分析
查看>>
jdk keytools for spring-boot
查看>>
百度前端学习日记03——CSS选择器
查看>>
二维数组和二级指针
查看>>
HDOJ_就这么个烂题总是WA先放这把
查看>>
十大经典官场小说
查看>>
stages
查看>>
uva 101 POJ 1208 The Blocks Problem 木块问题 vector模拟
查看>>
Python 面向对象 特殊方法(魔法方法)
查看>>