如何拆分读取线,但不要在撇号中拆分值?

发布于 2025-01-24 20:53:14 字数 863 浏览 3 评论 0原文

示例TXT文件

ADD 'Cordless Screwdriver' 30 1 2
COST 'Multi Bit Ratcheting'
FIND 'Thermostat'
FIND 'Hand Truck'
SELL 'Hammer' 1
QUANTITY 'Paint Can'
FIRE 'Joshua Filler'
HIRE 'Lewis hamilton' 35 G
PROMOTE 'Lewis hamilton' M
SCHEDULE

代码

File inputFile = new File("src/edu/iu/c212/resources/input.txt");
        String[] inputWords = null;
        FileReader inputReader = new FileReader(inputFile);
        BufferedReader bri = new BufferedReader(inputReader);
        String y;
        while ((y = bri.readLine()) != null) {
            inputWords = y.split(" ");

--- Project Code That Handles Split Up Lines ---

}

有没有办法在跨行线时可以在撇号中使用拆分拆分项目?这样,无论第一项是一个或两个单词,如果我调用输入字[1],它将始终返回完整的字符串。

发生了什么:“多重棘轮” - >输入字[1] - > 'Multi

我想要的:“ Multi bit ratcheting” - >输入字[1] - > “多重棘轮”

Example txt file

ADD 'Cordless Screwdriver' 30 1 2
COST 'Multi Bit Ratcheting'
FIND 'Thermostat'
FIND 'Hand Truck'
SELL 'Hammer' 1
QUANTITY 'Paint Can'
FIRE 'Joshua Filler'
HIRE 'Lewis hamilton' 35 G
PROMOTE 'Lewis hamilton' M
SCHEDULE

Code

File inputFile = new File("src/edu/iu/c212/resources/input.txt");
        String[] inputWords = null;
        FileReader inputReader = new FileReader(inputFile);
        BufferedReader bri = new BufferedReader(inputReader);
        String y;
        while ((y = bri.readLine()) != null) {
            inputWords = y.split(" ");

--- Project Code That Handles Split Up Lines ---

}

Is there a way I can have the split not split items within the apostrophes when going across the line? That way regardless if the first Item is one word or two words, if I call inputWords[1] It will always return the full string.

What happens: "Multi Bit Ratcheting" -> inputWords[1] -> 'Multi

What I want: "Multi Bit Ratcheting" -> inputWords[1] -> 'Multi Bit Ratcheting'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2025-01-31 20:53:14

您可以使用模式'。*?'| \ s+

String line = "ADD 'Cordless Screwdriver' 30 1 2";
String[] matches = Pattern.compile("'.*?'|\\S+")
                      .matcher(line)
                      .results()
                      .map(MatchResult::group)
                      .toArray(String[]::new);
System.out.println(Arrays.toString(matches));
// [ADD, 'Cordless Screwdriver', 30, 1, 2]

您可以将上述逻辑应用于文件中的每一行。但是,您应该定义循环外部图案,以便不必为每行重新编译。

您更新的代码:

File inputFile = new File("src/edu/iu/c212/resources/input.txt");
FileReader inputReader = new FileReader(inputFile);
BufferedReader bri = new BufferedReader(inputReader);
Pattern r = Pattern.compile("'.*?'|\\S+");
String y;
while ((y = bri.readLine()) != null) {
    List<String> items = new ArrayList<>();
    Matcher m = r.matcher(y);
    while (m.find()) {
        items.add(m.group());
    }

    // use the list here...
}

You could apply a regex find all to each line using the pattern '.*?'|\S+:

String line = "ADD 'Cordless Screwdriver' 30 1 2";
String[] matches = Pattern.compile("'.*?'|\\S+")
                      .matcher(line)
                      .results()
                      .map(MatchResult::group)
                      .toArray(String[]::new);
System.out.println(Arrays.toString(matches));
// [ADD, 'Cordless Screwdriver', 30, 1, 2]

You may apply the above logic to each line from the file. You should, however, define the pattern outside the loop so that it doesn't have to be recompiled for every line.

Your updated code:

File inputFile = new File("src/edu/iu/c212/resources/input.txt");
FileReader inputReader = new FileReader(inputFile);
BufferedReader bri = new BufferedReader(inputReader);
Pattern r = Pattern.compile("'.*?'|\\S+");
String y;
while ((y = bri.readLine()) != null) {
    List<String> items = new ArrayList<>();
    Matcher m = r.matcher(y);
    while (m.find()) {
        items.add(m.group());
    }

    // use the list here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文