Skip site navigation (1)Skip section navigation (2)
Date:      07 Jul 1999 21:31:10 +0200
From:      Dag-Erling Smorgrav <des@flood.ping.uio.no>
To:        Jamie Howard <howardjp@wam.umd.edu>
Cc:        Archie Cobbs <archie@whistle.com>, freebsd-hackers@FreeBSD.ORG, tech-userlevel@netbsd.org, tech@openbsd.org
Subject:   Re: Repalcement for grep(1)
Message-ID:  <xzpzp18s1dt.fsf@flood.ping.uio.no>
In-Reply-To: Jamie Howard's message of "Sun, 4 Jul 1999 21:32:22 -0400 (EDT)"
References:  <Pine.GSO.4.10.9907042126350.23842-100000@rac9.wam.umd.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
Jamie Howard <howardjp@wam.umd.edu> writes:
> On Sun, 4 Jul 1999, Archie Cobbs wrote:
> >        There are two special cases- of bracket  expressions:  the
> >        bracket expressions `[[:<:]]' and `[[:>:]]' match the null
> >        string at the beginning and end of a word respectively.
> > Perhaps this will help with -w?
> Yes, I received a patch from Simon Burge which implements this.  It also
> beats using [^A-Za-z] and [A-Za-z$] as I was and GNU grep does.

No, because there are scripts out there (e.g. ports/Mk/bsd.port.mk)
which rely on this behaviour.

I suggest you explore the magic of the nmatch and pmatch arguments to
regexec() :) Specifically, the pattern matched a word if:

  ((pmatch[0].rm_so == 0 || !isalpha(line[pmatch[0].rm_so-1]))
   && (pmatch[0].rm_eo == len || !isalpha(line[pmatch[0].rm_eo])))

This is off the top of my head, from reading the man page: you'll have
to try it out to see if it works.

You might want to replace isalpha with something less restrictive,
such as isalnum(), or:

#define isword(x) (isalnum(x) || (x) == '_')

(judging from empirical observation, the latter corresponds to what
GNU grep does)

As for full-line matches (-x), simply check that

  (pmatch[0].rm_so == 0 && pmatch[0].rm_eo == len)

This should save you from playing games with back-references.

(both code snippets assume that line points to a line of text from the
input and that len is the length of that line minus the newline)

DES
-- 
Dag-Erling Smorgrav - des@flood.ping.uio.no


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?xzpzp18s1dt.fsf>