读取包含空格的字符串值:Java

读取包含空格的字符串值:Java,java,Java,我需要从用冒号分隔的文件中读取空格分隔的值 我的文件中有这些数据- Name : User123 DOB : 1/1/1780 Application Status : Not approved yet 当前实现:我将分隔符(在我的例子中是冒号)之后的所有值复制到一个新文件中,并相应地从新文件中读取值 将条目复制到新文件空间时将被忽略。在上述文件中,“尚未批准”仅保存为“未批准”。我怎样才能得到完整的线路?这是我的密码- String regex = "\\b(Name |DOB |

我需要从用冒号分隔的文件中读取空格分隔的值

我的文件中有这些数据-

Name : User123
DOB : 1/1/1780
Application Status : Not approved yet
当前实现:我将分隔符(在我的例子中是冒号)之后的所有值复制到一个新文件中,并相应地从新文件中读取值

将条目复制到新文件空间时将被忽略。在上述文件中,“尚未批准”仅保存为“未批准”。我怎样才能得到完整的线路?这是我的密码-

String regex = "\\b(Name |DOB | Application Status )\\s*:\\s*(\\S+)";
    
    Pattern p = Pattern.compile(regex);
    try (BufferedReader br = new BufferedReader(new FileReader("<file to read data>"));
         BufferedWriter bw = new BufferedWriter(new FileWriter("<copy the new file here>"))) 
    {
        String line;
      
        while ((line = br.readLine()) != null) {
            Matcher m = p.matcher(line);
            if (m.find())
                bw.write(m.group(2) + '\n');
        }
    }
    
    String st;
    int count = -1;
    String[] data = new String[100];
    
    File datafile =new File("<new file where data is copied>");   
    
    try
    {
        Scanner sc = new Scanner(datafile);
        while(sc.hasNextLine())
        {
            data[++count] = sc.nextLine();
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
String regex=“\\b(名称| DOB |应用程序状态)\\s*:\\s*(\\s+);
Pattern p=Pattern.compile(regex);
try(BufferedReader br=newbufferedreader(newfilereader)(“”);
BufferedWriter bw=新的BufferedWriter(新文件写入程序(“”))
{
弦线;
而((line=br.readLine())!=null){
匹配器m=p.匹配器(线);
if(m.find())
write(m.group(2)+'\n');
}
}
字符串st;
整数计数=-1;
字符串[]数据=新字符串[100];
文件数据文件=新文件(“”);
尝试
{
扫描仪sc=新扫描仪(数据文件);
while(sc.hasNextLine())
{
数据[++计数]=sc.nextLine();
}
}
捕获(例外e)
{
系统输出打印ln(e);
}

\\S+
在regex
中“\\b(名称| DOB |应用程序状态)\\S*:\\S*(\\S+”仅获取非空白字符。因此,它在看到
“Not”
值后的空格后终止。为了获得
”之后的完整值:“
\\S+
更改为
*
,它可以获取除换行符以外的任何字符。因此正则表达式变成如下
“\\b(Name | DOB | Application Status)\\s*:\\s*(.*)”
。它在值之后获得所有空间,因此在使用该值之前修剪该值。所以你的代码变成这样

String regex = "\\b(Name |DOB | Application Status )\\s*:\\s*(.*)";

Pattern p = Pattern.compile(regex);
try (BufferedReader br = new BufferedReader(new FileReader("<file to read data>"));
     BufferedWriter bw = new BufferedWriter(new FileWriter("<copy the new file here>"))) 
{
    String line;
  
    while ((line = br.readLine()) != null) {
        Matcher m = p.matcher(line);
        if (m.find())
            bw.write(m.group(2) + '\n');
    }
}

String st;
int count = -1;
String[] data = new String[100];

File datafile =new File("<new file where data is copied>");   

try
{
    Scanner sc = new Scanner(datafile);
    while(sc.hasNextLine())
    {
        data[++count] = sc.nextLine().trim();
    }
}
catch(Exception e)
{
    System.out.println(e);
}
String regex=“\\b(名称| DOB |应用程序状态)\\s*:\\s*(*)”;
Pattern p=Pattern.compile(regex);
try(BufferedReader br=newbufferedreader(newfilereader)(“”);
BufferedWriter bw=新的BufferedWriter(新文件写入程序(“”))
{
弦线;
而((line=br.readLine())!=null){
匹配器m=p.匹配器(线);
if(m.find())
write(m.group(2)+'\n');
}
}
字符串st;
整数计数=-1;
字符串[]数据=新字符串[100];
文件数据文件=新文件(“”);
尝试
{
扫描仪sc=新扫描仪(数据文件);
while(sc.hasNextLine())
{
数据[++计数]=sc.nextLine().trim();
}
}
捕获(例外e)
{
系统输出打印ln(e);
}
您的正则表达式(
(\\S*)
)不会捕获分隔符后面的“所有值”,只捕获第一个单词。请尝试使用
(.*)
。或者只使用
line.split(\\s*:\\s*”
String.split(“:”
和一些
String.trim()