Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to do negative Lookaround with AWK?

328 views
Skip to first unread message

Marc de Bourget

unread,
Feb 15, 2016, 4:19:32 PM2/15/16
to
To begin with, a definition of "Lookaround" taken from the
"Regular Expressions Pocket Reference" by Tony Stubblebine:

>Begin quotation.
Lookahead: (?=...), (?!...)
Lookbehind: (?<=...), (?<!...)

Lookaround constructs match a location in the text where the subpattern
would match (lookahead), would not match (negative lookahead), would have
finished matching (lookbehind), or would not have finished matching
(negative lookbehind).
For example, foo(?=bar) matches foo in foobar but not food.
Implementations often limit lookbehind constructs to subpatterns
with a predetermined length.
>End quotation.

With Ruby, you can do things like:
puts str.gsub(/(?<!#{CONST_ALPHANUMERIC_ONESIGN})op,(?=[0-9])/, "op.")

This means: gsub "op," by "op.", but only if it is *not* preceded by an
alphanumeric sign = negative Lookbehind: (?<!...) and followed by numbers
= positive Lookahead (?=...). Negative Lookaround uses "!" instead of "=".

I think "Positive Lookaround" can be achieved by grouping with parentheses
with gensub or match functions. Is it possible to do "Negative Lookaround"?

Aharon Robbins

unread,
Feb 16, 2016, 4:37:31 PM2/16/16
to
In article <42fe6450-088b-4c80...@googlegroups.com>,
Marc de Bourget <marcde...@gmail.com> wrote:
>I think "Positive Lookaround" can be achieved by grouping with parentheses
>with gensub or match functions. Is it possible to do "Negative Lookaround"?

No.

You may wish to switch to a different language such as Perl or Ruby.
--
Aharon (Arnold) Robbins arnold AT skeeve DOT com

Doc Trins O'Grace

unread,
Feb 17, 2016, 3:06:21 PM2/17/16
to
That's an interesting idea, Marc. I'll be interested in following this thread. Although, not if the only solution is to point to some other language. What's the fun in that?

Marc de Bourget

unread,
Feb 21, 2016, 7:50:53 AM2/21/16
to
Thank you Doc Trins for you interest.
It seems Lookaround like described above isn't possible
and it is too complicated to implement as a awk feature.

Doc Trins O'Grace

unread,
Feb 24, 2016, 8:22:38 AM2/24/16
to
Maybe not as a language feature, but it looks like it might
be done as user defined function.

In something like 42 years in this field, I still get a lot
of pleasure (perverse, perhaps) in twisting a language's arm.
:-)

Aharon Robbins

unread,
Feb 24, 2016, 12:53:56 PM2/24/16
to
In article <215e4698-1b88-4e07...@googlegroups.com>,
Please post the code when you're done. :-)

Arnold

Marc de Bourget

unread,
Jan 5, 2017, 2:10:10 PM1/5/17
to
Concerning the user defined function, I think Ed Mortons's very good gsubs
function https://groups.google.com/forum/#!topic/comp.lang.awk/uPkta6saVoU
might be a good starting point. Lookaround is most times needed with
substitution, so an additional argument could be added to the function
to take care of surrounding borders and ignore or require them.
If I have a lot of free time I'll try to implement it.

Marc de Bourget

unread,
Jan 6, 2017, 1:15:16 AM1/6/17
to
Le mercredi 24 fÄCvrier 2016 18:53:56 UTC+1, Aharon Robbins a ÄCcritâ :
0 new messages