----------------解决方案--------------------------------------------------------
[CODE]String s="61.54.231.245 61.54.231.245 河南省安阳市 新世纪网吧 ";
Matcher m=Pattern.compile("\\d\\s+").matcher(s);
while(m.find()){
String temp=m.group();
s=s.replaceAll(temp,temp.trim()+",");
}
System.out.println(s);[/CODE]
看看这个,它可以实现
----------------解决方案--------------------------------------------------------
你可以把你所有的记录让它来替换
它只替换数字后面的空格成逗号,其它地方的空格不替换
----------------解决方案--------------------------------------------------------
好,明白了..
\\d\\s+ 这句非常适用..我怎么想不到呢.
s=s.replaceAll(temp,temp.trim()+",");
这句我有点儿不明白..temp.trim()不是返回不包含空格的其它字符吗?这样的意思是说把空格去掉并把 空格处换成逗号吗?
----------------解决方案--------------------------------------------------------
就是这个意思
先把空格前面有数字的匹配出来,然后把空格去掉,加上逗号,替换匹配到的字符串,就OK了
----------------解决方案--------------------------------------------------------
谢谢千里,我能看明白了.....
----------------解决方案--------------------------------------------------------
[CODE]String s="61.54.231.245 61.54.231.245 河南省安阳市 新世纪网吧 ";
Matcher m=Pattern.compile("\\d\\s+").matcher(s);
while(m.find()){
String temp=m.group();
s=s.replaceAll(temp,temp.trim()+",");
}
System.out.println(s);[/CODE]
看看这个,它可以实现
千里代码有问题了.您给我的这个方法可以按要求分开一部分,
但不知道为什么,同样的格式的字符串,同样的分割要求,有的用这个方法就不能按要求分好~
读这个文件:
里面全是格式类似为: 61.54.231.248 61.54.231.248 河南省安阳市 安阳师范学院
但有的可以正确转换有的就不成....麻烦帮忙再看下好吗?我真是找不出问题...
文件Test8.java
-----------------------------------------------------------------
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test8
{
public static void main(String[] args)
{
FileInput in = new FileInput("d:\\a.txt","d:\\cc.txt");
in.read();
}
}
class FileInput
{
private BufferedReader br;
private BufferedWriter bw;
public FileInput(String fileName,String outFileName)
{
try
{
br = new BufferedReader(new FileReader(fileName));
bw = new BufferedWriter(new FileWriter(outFileName));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void read() //读文件
{
String strIn;
try
{
while((strIn = br.readLine())!=null)
{
Matcher m = Pattern.compile("\\d\\s+").matcher(strIn);
if(m.find())
{
String out = m.group();
strIn = strIn.replaceAll(out,out.trim()+",");
System.out.println(strIn);
// toFormatIp(strIn);
write(strIn);
}
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void write(String str) //写入文件
{
try
{
bw.write(str);
bw.newLine();
bw.flush();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
----------------解决方案--------------------------------------------------------
哪个不能按要求分好?
是不是还有另外的格式?
----------------解决方案--------------------------------------------------------
我看了你的文件,都挺规范的啊
----------------解决方案--------------------------------------------------------
是的呀..你试下这个程序,运行都没问题,就是偏偏有的格式加不上逗号....
我也看不出为什么,所以再来请教你..
----------------解决方案--------------------------------------------------------