greedy regexes

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
pb4072
Posts: 14
Joined: Tue Nov 10, 2009 7:24 pm
Location: Washington, DC area

greedy regexes

Post by pb4072 »

Hi,
I've always thought that if I put a "?" after a search expression, that it would stop after the first instance of something. Doesn't seem to work for me, because, the whole line gets selected instead of just the first instance.

search <employee>(.*)?

gives me the whole line instead of just the first name.

Thanks,
Peter
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

TextPad's regex recogniser is old and weak, and many constructs supported by modern tools do not work in TextPad. In particular, TextPad's regex recogniser doesn't support non-greedy repetition operators (such as *?).

Note that your regex (.*)? does not contain a non-greedy repetition operator: it's just an optional occurrence (?) of any number (possibly zero) (*) of occurrences of any character (.). It's equivalent to .* (except that it's slower).

The non-greedy forms of the operators, where they are supported, are *?, +? and ?? (without intervening parentheses).

What are you trying to do?
pb4072
Posts: 14
Joined: Tue Nov 10, 2009 7:24 pm
Location: Washington, DC area

greed and non-greedy regexes

Post by pb4072 »

Thanks, Ben. My example wasn't very good. I'm trying to parse through a comma-delimited Excel file, to try and make it into XML. So, I was trying to capture the first object in the line, in this case "employee," followed by its content, up to the beginning of the second object. So, your description helps me. I was just doing it wrong. I probably need "*?."
Thanks again,
Peter
ben_josephs
Posts: 2461
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

*?. is not a regex.

Are you looking for
employee,[^,]*
?
pb4072
Posts: 14
Joined: Tue Nov 10, 2009 7:24 pm
Location: Washington, DC area

greed and non-greedy regexes

Post by pb4072 »

Probably. I'm at home now. I do this stuff at work. When I'm back to work Monday I'll try that. That looks correct.
Post Reply