如何在第一个选项卡实例后删除所有字符?

问题描述 投票:-4回答:2

我有一个大文本文件,大约200,000行单词翻译。我想保留在标签后面显示的翻译文本。

abaxial van  osovine
abbacy  opatstvo
abbaino     kora
abbatial    opatski
abbe    opat
abbé    opat
abbé    sveæenik
hematological parameters    hematološki pokazatelji

如何在第一个选项卡实例之前删除所有字符?

java inputstream bufferedreader bufferedwriter inputstreamreader
2个回答
2
投票

您可以使用此正则表达式匹配翻译前的所有内容:

 .+? {2,}

在线试用这个正则表达式:https://regex101.com/r/P0TY1k/1

使用此正则表达式在您的字符串上调用replaceAll

yourString.replaceAll(".+? {2,}", "");

编辑:如果分隔符不是2个空格而是选项卡,您可以尝试使用此正则表达式:

.+?(?: {2,}|\t)

0
投票

所以你可以使用正则表达式非常有效地进行字符串操作。

import java.util.regex.Matcher; import java.util.regex.Pattern;

公共类Main {

/**
 * Splits the line related to translation into 2 groups by splitting it on
 * two spaces " " and storing the splits into two named groups (key,
 * value)</br>
 * Group1 (key) is the text before the two spaces.</br>
 * Group2 (value) is the text after the two spaces.</br>
 */
private static final Pattern TRANSLATION_PATTERN = Pattern.compile("<key>.*)\\s\\s+(<value>.*)");

public static String grabTextAfterTwoSpaces(String input) {
    Matcher matcher = TRANSLATION_PATTERN.matcher(input);

    /*
     * You have to call .matches() for the regex to actually be applied.
     */
    if (!matcher.matches()) {
        throw new IllegalArgumentException(String.format("Provided input:[%s] did not contain two spaces", input));
    }

    return matcher.group("value");
}

public static void main(String[] args) {
    System.out.println(grabTextAfterTwoSpaces("abaxial van  osovine"));
    System.out.println(grabTextAfterTwoSpaces("abbacy  opatstvo"));
    System.out.println(grabTextAfterTwoSpaces("abbaino     kora"));
    System.out.println(grabTextAfterTwoSpaces("abbatial    opatski"));
    System.out.println(grabTextAfterTwoSpaces("abbe    opat"));
    System.out.println(grabTextAfterTwoSpaces("abbé    opat"));
    System.out.println(grabTextAfterTwoSpaces("abbé    sveæenik"));
    System.out.println(grabTextAfterTwoSpaces("abbacy  opatstvo"));

    System.out.println(grabTextAfterTwoSpaces("hematological parameters    hematološki pokazatelji"));
}

}

Try it online!

因此,如果您对该组使用“值”,您将获得2+空格后的所有内容。

僧院

完成了

方丈

方丈

方丈

sveæenik

僧院

血液学指标

© www.soinside.com 2019 - 2024. All rights reserved.