我的pgrm以字符串格式从网页返回新闻。我想在每次看到日期格式为dd-mm-yyyy时拆分字符串。是否有可能在每次看到日期时拆分???
Pattern patern=Pattern.compile("-");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
我的数据格式是这样的
String s="Meow looks like cocaine, is 20 times cheaper, more dangerous, and not illegal; with children as young as 14 addicted to it, CM Devendra Fadnavis has asked the Centre to ban it under the Narcotics Act 18-Dec-2014l The girl was allegedly raped by a school supervisor last week; an activist wrote to the DCP following which the police approached and convinced the survivor’s family to register the offence 18-Dec-2014 Former principal of a Mumbai school has written to education minister against reinstatement of a teacher, accused of sexually harassing nearly 40 Std X students, who is now out on bail 19-Dec-2014"
答案 0 :(得分:2)
使用该模式,它将在每个“ - ”上拆分,但第1组甚至不存在,因为您在正则表达式中不使用任何组。
使用String.split
,即
String[] split = s.split("\\d{2}-\\w{3}-\\d{4}");
for (String x :split)
{
System.out.println(x);
}
正则表达式解释:2个数字后跟一个短划线,然后是3个单词字符(几个月后的短名称都是3个字母,后面是另一个短划线,年份是4个数字。
答案 1 :(得分:1)
您可以尝试以下代码块。这将添加换行符。
String s="Meow looks like cocaine, is 20 times cheaper, more dangerous, and not illegal; with children as young as 14 addicted to it, CM Devendra Fadnavis has asked the Centre to ban it under the Narcotics Act 18-Dec-2014l The girl was allegedly raped by a school supervisor last week; an activist wrote to the DCP following which the police approached and convinced the survivor’s family to register the offence 18-Dec-2014 Former principal of a Mumbai school has written to education minister against reinstatement of a teacher, accused of sexually harassing nearly 40 Std X students, who is now out on bail 19-Dec-2014 ";
Pattern pattern = Pattern.compile("(([0-9]{2})-([a-zA-Z]{3})-([0-9]{4}))");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
s = s.replaceAll(matcher.group(1),matcher.group(1)+"\n");
}
System.out.println(s);
答案 2 :(得分:1)
看到你的日期格式是dd-Monyyyy,这是我提出的正则表达式: ' [0123] [\ d] - [JFMASOND] [aepuco] [nbrylgptvc] - \ d \ d \ d \ d'
[\ d]匹配日期,在十位数中可以有0,1,2或3,在那里它可以有任何小数
[JFMASOND] [aepuco] [nbrylgptvc]与月份相匹配,我从不同的月份开始使用字母表,第一个字母表中的字母数量为2个。
\ d \ d \ d \ d与年份匹配(0000-9999) 希望这很清楚。这个正则表达式在python中运行,没有在java中测试但它应该可以正常工作。
String exp='[0123][\d]-[JFMASOND][aepuco][nbrylgptvc]-\d\d\d\d';
String[] split = s.split(exp)
for (String x :split)
{
System.out.println(x);
}