I have a string like below:
Movies(s):
DIE ANOTHER DAY
TOMORROW NEVER DIES
WORLD IS NOT ENOUGH
Running Date(s):
I want to extract the movie names as separate matches not as a whole like below:
Match 1: DIE ANOTHER DAY
Match 2: TOMORROW NEVER DIES
Match 3: WORLD IS NOT ENOUGH
I tried to use lookahead and lookbehind but couldn't succeed in getting three matches.
Here's a one-liner:
String[] movies = str.replaceAll(".*Movies\\(s\\):\\s*|Running Date\\(s\\):.*", "").split("[\n\r]+");
This code first strips off the front/back, leaving just the movie names, then splits on (platform independent) newline chars.
You can leverage the discard technique through a regex like this:
.*:|^(.+)$
The idea behind the discard technique is to use a chain of pattern that you want to get rid off. So, you can have something like this:
discard patt1 | discard patt2 | discard pattN | (capture this)
Applying this technique to your string, you can modify above regex to something like this:
Movies\(s\):|Running Date\(s\):|(.+)
discard--^ discard--^ capture--^
You can see easily with this diagram:
Match information
MATCH 1
1. [11-26] `DIE ANOTHER DAY`
MATCH 2
1. [27-46] `TOMORROW NEVER DIES`
MATCH 3
1. [47-66] `WORLD IS NOT ENOUGH`
You can use this Java code:
Pattern regex = Pattern.compile(".*:|^(.+)$", Pattern.MULTILINE);
// or this line:
// Pattern regex = Pattern.compile("Movies\\(s\\):|Running Date\\(s\\):|(.+)", Pattern.MULTILINE);
Matcher regexMatcher = regex.matcher("YOUR STRING HERE");
if (regexMatcher.find()) {
System.out.println(regexMatcher.group(1));
}
我通过使用以下正则表达式修复了它:
(?s)(?<=Movie\(s\)\:\s{0,3}\r{0,1}\n.{0,100})([A-Z \.]+)(?=.{0,100}Running\Date\(s\)\:)
String input = "Movies(s):\r\n" +
"DIE ANOTHER DAY\r\n" +
"TOMORROW NEVER DIES\r\n" +
"WORLD IS NOT ENOUGH\r\n" +
"Running Date(s):";
Pattern pattern = Pattern.compile("(([A-Z ]+)[\r\n]{1,2})");
Matcher m = pattern.matcher(input);
int index = 0;
while(m.find())
{
System.out.println(++index + "," + m.group(2));
}
And the output will be(tested):
1,DIE ANOTHER DAY
2,TOMORROW NEVER DIES
3,WORLD IS NOT ENOUGH
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.