从一行得到特定的词

时间:2014-01-30 09:50:55

标签: java

如何从这些行中获取OTHER和TREATED之间的单词(aaa,bbbb,ccc..etc),OTHER和TREATED是常见的行。我想只在OTHER和TREATED单词之间显示。

TABLE 5L : ESTIMATED AREA UNDER OTHER aaaa TREATED WITH PSB BY SIZE GROUPS.
TABLE 6L : ESTIMATED AREA UNDER OTHER bbbbb  TREATED WITH PSB BY SIZE GROUPS.
TABLE 7L : ESTIMATED AREA UNDER OTHER ccccc TREATED WITH PSB BY SIZE GROUPS.
TABLE 8L : ESTIMATED AREA UNDER OTHER dddddd  TREATED WITH PSB BY SIZE GROUPS.
TABLE 9L : ESTIMATED AREA UNDER OTHER eeeee  TREATED WITH PSB BY SIZE GROUPS.
TABLE 10L: ESTIMATED AREA UNDER OTHER ffffff  TREATED WITH PSB BY SIZE GROUPS.
TABLE 11L: ESTIMATED AREA UNDER OTHER gggg  TREATED WITH PSB BY SIZE GROUPS.
TABLE 12L: ESTIMATED AREA UNDER OTHER hhhh TREATED WITH PSB BY SIZE GROUPS.
TABLE 13L: ESTIMATED AREA UNDER OTHER llll  TREATED WITH PSB BY SIZE GROUPS.

3 个答案:

答案 0 :(得分:2)

当您阅读该行后,您可以拆分该字符串。你需要使用Stringarray

Stringarray = String.split("OTHER ")
Endresult = Stringarray[1].split(" TREATED")

Endresult [0]将是你aaaa bbbb cccc或其他任何东西

答案 1 :(得分:1)

使用PatternMatcher

尝试以下

String str="TABLE 5L : ESTIMATED AREA UNDER OTHER aaaa TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 6L : ESTIMATED AREA UNDER OTHER bbbbb  TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 7L : ESTIMATED AREA UNDER OTHER ccccc TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 8L : ESTIMATED AREA UNDER OTHER dddddd  TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 9L : ESTIMATED AREA UNDER OTHER eeeee  TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 10L: ESTIMATED AREA UNDER OTHER ffffff  TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 11L: ESTIMATED AREA UNDER OTHER gggg  TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 12L: ESTIMATED AREA UNDER OTHER hhhh TREATED WITH PSB BY SIZE GROUPS." +
            "TABLE 13L: ESTIMATED AREA UNDER OTHER llll  TREATED WITH PSB BY SIZE GROUPS.";


    Pattern pattern = Pattern.compile("OTHER(.*?)TREATED");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }

正则表达式将搜索"OTHER""TREATED"之间的任何字符串。

答案 2 :(得分:0)

试试这个

String w = str.replaceAll(".+ OTHER (.+) TREATED .+", "$1");

这意味着用括号中表达式的组1的内容替换整个字符串