我有一个大文本文件,大约200,000行单词翻译。我想保留翻译文本,该文字出现在标签之后。
abaxial van osovine
abbacy opatstvo
abbaino kora
abbatial opatski
abbe opat
abbé opat
abbé sveæenik
hematological parameters hematološki pokazatelji
如何在第一个标签实例之前删除所有字符?
答案 0 :(得分:2)
您可以使用此正则表达式匹配翻译前的所有内容:
.+? {2,}
在线试用此正则表达式:https://regex101.com/r/P0TY1k/1
使用此正则表达式在字符串上调用replaceAll
。
yourString.replaceAll(".+? {2,}", "");
编辑:如果分隔符不是2个空格而是选项卡,则可以尝试使用此正则表达式:
.+?(?: {2,}|\t)
答案 1 :(得分:0)
所以你可以使用正则表达式来操作非常有效的字符串。
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class 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"));
}
}
因此,如果您对该组使用“值”,您将获得2+空格后的所有内容。
osovine
opatstvo
戈拉
opatski
opat
opat
sveæenik
opatstvo
hematološkipokazatelji