简体   繁体   English

正则表达式提取由换行符分隔的两个字符串之间的字符串

[英]Regex to extract string between two strings separated by newline

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: 您可以通过如下的正则表达式利用丢弃技术:

.*:|^(.+)$

Working demo 工作演示

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--^

Working demo 工作演示

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: 您可以使用以下Java代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM