简体   繁体   中英

Java Regular Expression to fetch particular String from a line of sentence in a file

I need to read a file and fetch only the file names ending with .csv. the file will contain several lines like this below

-dataFileName ABC.csv -command ii
-dataFileName EFG.csv -command ii
-dataFileName HIJ.csv -command ii
-dataFileName MNPQR.csv -command ii
-dataFileName UVXYZ.csv -command ii

We can see that the -dataFileName [ XXXX ] -command ii is kind of repetitive

I want ABC .csv , EFG .csv , HIJ .csv , MNPQR .csv , UVXYZ .csv ,as my console output.

If you simply want to leverage the repetition of -dataFileName and -command ii in your strings then you can simple do this in Java,

replaceAll("-dataFileName| -command ii", "")

and write code something like this,

public static void main(String args[]) throws Exception {
    List<String> list = Arrays.asList(
            "-dataFileName ABC.csv -command ii",
            "-dataFileName EFG.csv -command ii",
            "-dataFileName HIJ.csv -command ii",
            "-dataFileName MNPQR.csv -command ii",
            "-dataFileName UVXYZ.csv -command ii"
    );

    list.forEach(x -> {System.out.println(x + " --> " + x.replaceAll("-dataFileName| -command ii", ""));});
}

This gives following output,

-dataFileName ABC.csv -command ii -->  ABC.csv
-dataFileName EFG.csv -command ii -->  EFG.csv
-dataFileName HIJ.csv -command ii -->  HIJ.csv
-dataFileName MNPQR.csv -command ii -->  MNPQR.csv
-dataFileName UVXYZ.csv -command ii -->  UVXYZ.csv

If you don't like that, you can use this simple regex to do the job,

-dataFileName (.*?) -command ii

and capture group 1.

Demo

I don't see why you want to use a regex for this. You can easily write a simple parser for it that won't cause problem when your requirements change (need to handle quotes? easy enough with a parser, messy with a regex).

An example program that would do this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.stream.Stream;

class Scratch {

    private static final String INPUT = "-dataFileName ABC.csv -command ii\n" +
        "-dataFileName EFG.csv -command ii -dataFileName OAZE.csv\n" +
        "-dataFileName HIJ.csv -command ii\n" +
        "-dataFileName MNPQR.csv -command ii\n" +
        "-dataFileName UVXYZ.csv -command ii";

    public static void main(String[] args) throws IOException {
        try (BufferedReader reader = new BufferedReader(new StringReader(INPUT))) {
            reader.lines()
                .flatMap(line -> fetchFilenamesFromArgumentLine(line, "dataFileName", "csv"))
                .forEach(System.out::println);
        }
    }

    public static Stream<String> fetchFilenamesFromArgumentLine(String line, String argumentName, String extension) {
        Stream.Builder<String> resultBuilder = Stream.builder();

        int index = 0;
        String actualArgumentName = "-" + argumentName + " ";

        while ((index = line.indexOf(actualArgumentName, index)) >= 0) {
            int start = index + actualArgumentName.length();
            int end = line.indexOf(extension, start) + extension.length();

            resultBuilder.add(line.substring(start, end));
            index = end;
        }
        return resultBuilder.build();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM