-3

I need to find all strings that occur between the words "Close" and "Minor", for example, but the words "Close" and "Minor" occur many times in my string. How can I use regex to find all of the strings between each instance of "Close" and "Minor". Example:

In the text

$string = "Minor fall major fifth Close to home Minors cannot vote before 18 Polls Close at 8";

I want to match

 fall major fifth

and

s cannot vote before 18 Polls

I have tried to use this:

my @matches = $string =~ /Minor(.*) Close/gi;

but this matches

Minor fall major fifth Close to home Minors cannot vote before 18 Polls Close

which matches the words themselves, and does not produce the desired match.

I'm so lost!

Aditya J.
  • 131
  • 2
  • 11
  • I'm fascinated to know what strings you're expecting. Your example reads like the lyrics of "Hallelujah": ***The minor fall and the major lift / The baffled king composing "Hallelujah"*** – Borodin Nov 18 '16 at 23:48

1 Answers1

0

Use a non-greedy quantifier:

use strict;
use warnings;
use v5.10;

my $string = "Minor fall major fifth Close to home Minors cannot vote before 18 Polls Close at 8";

my @matches = $string =~ /Minor(.*?) Close/gi;

say for @matches;

Outputs:

 fall major fifth
s cannot vote before 18 Polls
Miller
  • 34,962
  • 4
  • 39
  • 60
  • this helps a ton, but I'm still matching the words themselves. I should be able to fix that on my own though – Aditya J. Nov 18 '16 at 22:02