Searching for all regular expression matches is greedy and not greedy!

Take the following line: "Marketing and cricket on the Internet."

I would like to find all possible matches for "Ma" -any text- "et" using a regular expression. So..

  • Market
  • Marketing and cricket
  • Internet Marketing and Cricket

The regular expression Ma.*etreturns "Marketing and cricket on the Internet." The regular expression Ma.*?etreturns Market. But I would like the regex to return all 3. Is this possible?

Thank.

+3
source share
4 answers

As far as I know: None.

, regexp , . :

Ma.*?et
Ma.{3,}?et

... ...

+2

, , . PHP:

function preg_match_ubergreedy($regex,$text) {

    for($i=0;$i<strlen($text);$i++) {
        $exp = str_replace("*","{".$i."}",$regex);
        preg_match($exp,$text,$matches);
        if($matches[0]) {
            $matched[] = $matches[0];
        }
    }

    return $matched;

}
$text = "Marketing and Cricket on the Internet";
$matches = preg_match_ubergreedy("@Ma.*?et@is",$text);
+1

, POSIX, ( ). , , , , , .

0

, , , . Marketing and Cricket on the Internet arketing and Cricket on the Internet, Marketing and Cricket on the Interne .

# - ...

public static IEnumerable<Match> SubMatches(Regex r, string input)
{
    var result = new List<Match>();

    var matches = r.Matches(input);
    foreach (Match m in matches)
    {
        result.Add(m);

        if (m.Value.Length > 1)
        {
            string prefix = m.Value.Substring(0, m.Value.Length - 1);
            result.AddRange(SubMatches(r, prefix));

            string suffix = m.Value.Substring(1);
            result.AddRange(SubMatches(r, suffix));
        }

    }

    return result;
}

, , Marmoset Marketing and Marmosets on the Internet, Marketing and Marmosets on the Internet, Marmosets on the Internet.

0

Source: https://habr.com/ru/post/1772939/


All Articles