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

From: John Clements (clements at brinckerhoff.org)
Date: Wed May 8 15:10:40 EDT 2013

On May 8, 2013, at 11:54 AM, Eli Barzilay wrote:

> 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")
>  '("some=text=here===")
>  -> (regexp-match #rx".*===" "some=text=here===but=not=this===text")
>  '("some=text=here===but=not=this===")
> 
> (But they can lead to dangerous inefficiencies.)

Interestingly, I searched for "greedy" on the regexp page--looking for exactly this--and didn't find it.  Ah… I see that it's called "shortest possible". Would it make sense to add "(non-greedy)" to this description?

John



Posted on the users mailing list.