字符串格式如下
[1234] hello
[56789] world
[112233] !!!!难道要用类似下面的方法吗
switch
case '['
case ']'
substring有简单的方法没
正则表达式 \\[(\\d+?)\\] \\d+? \\d*都不行啊
[1234] hello
[56789] world
[112233] !!!!难道要用类似下面的方法吗
switch
case '['
case ']'
substring有简单的方法没
正则表达式 \\[(\\d+?)\\] \\d+? \\d*都不行啊
解决方案 »
- 使用ImageIO.read读取JPG文件时,显示出来的图片会变黑?
- JAVA面试题解惑系列(六)——字符串(String)杂谈
- java swing 程序在宽屏和正常屏幕的显示不一样
- 弱弱地问
- 将java文件转换成exe文件
- 怎么对另一个文件的对象进行操作。
- 如何从根节点删除一个tree并重新建立一个新的tree
- 超级简单的问题!!!
- 关于applet怎样才能不挡住移过的图片?
- 用什么软件才能打开后缀为.z的文件?
- 请教一个关于java.awt.Image与sun.awt.image.ToolkitImage,java.awt.image.BufferedImage的问题
- 嵌套循环蒙掉了 ,求助下 谢谢了 就31分了
Matcher matcher = numbers.matcher("[124] hello");
while (matcher.find()) {
System.out.println(matcher.group());
}
public static void main(String[] args) {
String s = "[56789] world";
String[] str = s.split("]");
String target = "";
if(str.length != 0) {
target = str[0].substring(1);
}
System.out.println(target);
}
}
不知道符合楼主 要求不
\\d+一样可以,关键在那个while(matcher.find())
就是尝试去寻找匹配的字符串,找到了就打印出来。
[2372] Le ciel obscure
[2949] La solitude qui nous rend la peine
[5603] La ceour brise
[6714] a cause qu’il y a vécu seul
[10256] L’amour est parti
[11372] il y a longtemp que je t'ai vu
[13124] C'est trop long
[14544] C'est incroyable que je peux vivre comme ca...
[20704] 阿桑 - 寂寞在唱歌
[25047] 天黑了
[27611] 孤独又慢慢割着
[31359] 有人的心又开始疼了
[38751] 爱很远了很久没再见了
[44835] 就这样竟然也能活着
[51140] 你听寂寞在唱歌
[54047] 轻轻的 狠狠的
[56327] 歌声是这么残忍
[60571] 让人忍不住泪流成河
[62803] 谁说的
[77766] 人非要快乐不可
[81166] 好像快乐由得人选择
[88744] 找不到的那个人来不来呢
[95534] 我会是谁的谁是我的
[101598] 你听寂寞在唱歌
[104526] 轻轻的 狠狠的
[106394] 歌声是这么残忍
[111118] 让人忍不住泪流成河
[115126] 你听寂寞在唱歌
[118260] 温柔的 疯狂的
[120089] 悲伤越来越深刻
[124115] 怎样才能够让它停呢
[130004] 寂寞在唱歌
[137193] 专辑:寂寞在唱歌
[139049] 演唱:阿桑
[154027] 你听寂寞在唱歌
[156925] 轻轻的 狠狠的
[159339] 歌声是这么残忍
[163521] 让人忍不住泪流成河
[165742] 你听寂寞在唱歌
[171025] 温柔的 疯狂的
[174089] 悲伤越来越深刻
[177304] 怎样才能够让它停呢
[180928] 你听寂寞在唱歌
[184192] 轻轻的 狠狠的
[185917] 歌声是这么残忍
[189376] 让人忍不住泪流成河
[194516] 你听寂寞在唱歌
[196906] 温柔的 疯狂的
[199513] 悲伤越来越深刻
[202532] 谁能帮个忙让它停呢
[206400] HE~
[215096] 天黑得
[223616] 像不会再天亮了
[225933] 明不明天也无所谓了
[230131] 就静静的看青春难依难舍
[239039] 泪还是热的泪痕冷了
[244503] HE~
但是[]内的是毫秒 现在想把[]内的数字替换成 标准的lrc格式的 分:秒:毫秒例如 第一句话 替换后就是
[00:02:02] Le ciel obscure
我写的数字转为分:秒:毫秒String timeconvert(String str) { Date date = new Date(Integer.parseInt(str));
SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
return sf.format(date);
}那位高手 能给点建议 有好的方法也可以说下
String str = input.next();
Pattern reg = Pattern.compile("\\d+");
Matcher matcher = reg.matcher(str);
while (matcher.find())
System.out.println(matcher.group());
public class PatternTest {
public static void main(String[] args) throws IOException{
String str = "[1234] hello";
for(String tmp:str.split("\\D+")){
if(tmp.length()>0){
System.out.println(tmp);
}
}
}
}只提取数字出来
public static void main(String[] args) throws IOException{
//歌词
String[] lyrics = {"[2372] Le ciel obscure",
"[2949] La solitude qui nous rend la peine",
"[5603] La ceour brise"};
ArrayList<String> numList = new ArrayList<String>();
//对每个歌词中的时间进行拆分
for(String str:lyrics){
numList.add(splitToNum(str));
}
//输出拆分后的时间
for(int i=0; i<numList.size(); i++){
System.out.println("第" + i + "个时间:"+timeconvert(numList.get(i)));
}
}
public static String splitToNum(String str){
for(String tmp:str.split("\\D+")){
if(tmp.length()>0){
return tmp;
}
}
return null;
}
public static String timeconvert(String str) { Date date = new Date(Integer.parseInt(str));
SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
return sf.format(date);
}
}
看这个http://topic.csdn.net/u/20110417/13/107bbf03-59ac-485d-addd-62eefe2e804a.html
public static void main(String[] args)
{
String s = "[2372] Le ciel obscure\r\n[56789] world";
Pattern p = Pattern.compile("\\[\\d+?\\]");
Matcher m = p.matcher(s);
while (m.find())
{
s = m.replaceFirst(toTime(m.group()));
m = p.matcher(s);
}
System.out.println(s); } private static String toTime(String num)
{
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
long time = Long.parseLong(num.substring(1, num.length() - 1));
return "[" + sdf.format(time) + ":" + ((time % 1000) / 10) + "]";
}
public static void main(String[] args) throws IOException{
String[] lyrics = "[2372] Le ciel obscure";
String newStr = lyrics.replaceFirst("\\[\\d+\\]","[" + timeconvert(splitToNum(lyrics)) + "]");
System.out.println(newStr);
}
public static String splitToNum(String str){
for(String tmp:str.split("\\D+")){
if(tmp.length()>0){
return tmp;
}
}
return null;
}
public static String timeconvert(String str) { Date date = new Date(Integer.parseInt(str));
SimpleDateFormat sf = new SimpleDateFormat("mm:ss:ms");
return sf.format(date);
}
}
String[] lyrics = "[2372] Le ciel obscure";
应该是String lyrics = "[2372] Le ciel obscure";
我原来用s = m.replaceALL(toTime(m.group()));
不行 没想到换成s = m.replaceFirst(toTime(m.group()));
就行了,,,o(︶︿︶)o 唉
谢谢 kyox0美女 你真热情呵感动
直接用表达式://[(//d+)//],然后用group(1),就没必要再substring了。
还有,在while里用StringBuilder比较好。
{
String s = "[2372] Le ciel obscure\r\n[56789] world";
Pattern p = Pattern.compile("\\[(\\d+?)\\]");
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find())
{
m.appendReplacement(sb, toTime(m.group(1)));
}
m.appendTail(sb);
System.out.println(sb); } private static String toTime(String num)
{
long time = Long.parseLong(num);
SimpleDateFormat sdt = new SimpleDateFormat("mm:ss");
return "[" + sdt.format(time) + ":" + ((time % 1000) / 10) + "]";
}
StringBuffer是个synchronized的methoed,性能不佳,但是mather里的appendReplacement只支持StringBuffer。
因为要优先考虑StringBuilder,所以appendReplacement就不能用了,只能考虑用StringBuilder里的method了。
现学现卖一下吧,下面是用StringBuilder的版本,在main里: String s = "[2372] Le ciel obscure\r\n[56789] world";
StringBuilder sb = new StringBuilder(s);
Pattern p = Pattern.compile("\\[(\\d+)\\]");
Matcher m = p.matcher(sb);
while (m.find())
{
sb.replace(m.start(), m.end(), toTime(m.group(1)));
}
System.out.println(sb);