My Java code:
String subjectString = "BYWW4 AterMux TP 46[_221] \n"
+ "FHTTY TC AterMux TP 9 \n"
+ "TUI_OO AterMux TP 2[_225] \n"
+ "F-UYRE TC AterMux TP 2 \n"
+ "RRRDSA AterMux TP 31[_256] ";
String textStr[] = subjectString.split("\n");
for (int i = 0; i < textStr.length; i++) {
String ResultString = null;
try {
Pattern regex = Pattern.compile("????????");
Matcher regexMatcher = regex.matcher(textStr[i]);
if (regexMatcher.find()) {
ResultString = regexMatcher.group();
System.out.println(ResultString); ///
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
}
I want the program to print the value after word (TP) and before ([) on this code to get result like below:
46
9
2
2
31
You can use regexp TP\\s*(\\d+)\\[
(double backslashes in Java code) and get a value with regexMatcher.group(1)
. But you should not recreate it on each iteration of the loop, you should use Pattern.compile
once per regexp.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.