-1

我有具有重复管道符号的字符串。

String Maindata="1370491695866|223|0|statement| select max(IF_ID) from IF_OPERATIONS.IF_Mediator|select max(IF_ID) from IF_OPERATIONS.IF_Mediator";

我需要提取字符串的最后一部分,即。

从最后一个“|”开始 “;”之前的符号 分号(包括“;”)。所以会是

从 IF_OPERATIONS.IF_Mediator 中选择 max(IF_ID);

我试过了,但因为“|” 符号重复我只得到

223|0|声明| 从 IF_OPERATIONS.IF_Mediator 中选择 max(IF_ID)|从 IF_OPERATIONS.IF_Mediator 中选择 max(IF_ID)

Pattern.compile("\\|([^;]*)").matcher(Maindata);

我也需要分号出现。如果有人知道这一点,这将是一个很大的帮助。

4

1 回答 1

3

You do not need regular expressions, it is not a miracle solution:

String mainData = "1370491695866|223|0|statement| select max(IF_ID) from IF_OPERATIONS.IF_Mediator|select max(IF_ID) from IF_OPERATIONS.IF_Mediator;";
String last = mainData.substring(mainData.lastIndexOf('|') + 1);
System.out.println(last);

Result:

run:
select max(IF_ID) from IF_OPERATIONS.IF_Mediator;
BUILD SUCCESSFUL (total time: 0 seconds)

If you might need the other tokens, you also have the split() method:

String[] tokens = mainData.split("\\|");
System.out.println(tokens[tokens.length - 1]);

The | is escaped because it has a special signification in regular expressions, and that's what split() uses, so is \ which is also escaped, hence \\|.

于 2013-06-06T06:58:01.790 回答