[racket] Regexp question: Pattern that can match everything to left of first pattern?

From: Eli Barzilay (eli at barzilay.org)
Date: Wed May 8 14:54:55 EDT 2013

An hour ago, John Clements wrote:
> The traditional way to do this would be to match a sequence of
> non-x's followed by an x, like this:
> #lang racket
> (regexp-match #rx"[^x]*x" "12x4x6") ; => '("12x")
> Things get a bit more interesting if you put a more complex pattern
> in place of a simple "x".

An easy hack is using non-greedy operators:

  -> (regexp-match #rx".*?===" "some=text=here===but=not=this===text")
  -> (regexp-match #rx".*===" "some=text=here===but=not=this===text")

(But they can lead to dangerous inefficiencies.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.