how can i convert this regex from java to c?

You can use an atomic group:

(?<!\bla\s+(?:petite\s+)?)(?:petite\s+)?(?>maison(?:e?s|x)?\b(?:\s+bleu[sxe]?)?)(?!\s+ciel)

See the .NET regex demo.

Details

  • (?<!\bla\s+(?:petite\s+)?) – a location in string not immediately preceded with a whole word la (\b is a word boundary) followed with 1+ whitespaces and an optional sequence of petite and 1+ whitespaces
  • (?:petite\s+)? – an optional petite and 1+ whitespaces sequence
  • (?>maison(?:e?s|x)?\b(?:\s+bleu[sxe]?)?)here, an atomic group that matches maison, then an optional sequence of an optional e followed with s or an x, then a word boundary and then an optional sequence of 1+ whitespaces and bleu followed with 1 or 0 s, x or e
  • (?!\s+ciel) – not followed with 1+ whitespaces and ciel.

See the C# demo:

var rx = @"(?<!\bla\s+(?:petite\s+)?)(?:petite\s+)?(?>maison(?:e?s|x)?\b(?:\s+bleu[sxe]?)?)(?!\s+ciel)";
var strings = new List<String> {" - maison"," - petite maison"," - maison bleue",
        " - petite maison bleue"," - la maison"," - maison ciel"," - la petite maison",
        " - maison bleue ciel"," - la petite maison bleue"," - petite maison bleue ciel",
        " - la maison bleue ciel"," - la petite maison ciel"," - la maison ciel"," - la petite maison bleue ciel"};
foreach (var s in strings)
{
    Console.WriteLine("{0} => \"{1}\"", s, Regex.Match(s, rx)?.Value);
}

Output:

 - maison => "maison"
 - petite maison => "petite maison"
 - maison bleue => "maison bleue"
 - petite maison bleue => "petite maison bleue"
 - la maison => ""
 - maison ciel => ""
 - la petite maison => ""
 - maison bleue ciel => ""
 - la petite maison bleue => ""
 - petite maison bleue ciel => ""
 - la maison bleue ciel => ""
 - la petite maison ciel => ""
 - la maison ciel => ""
 - la petite maison bleue ciel => ""

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top