--- Ronald J Kimball <[EMAIL PROTECTED]> wrote:
> On Wed, Jun 27, 2007 at 05:45:54AM -0700, Phil Carmody wrote:
> > Say I had a string satisfying /^[A-Z_]{6}$/, but not equal to '______'
> > and I wish to extract from that the 1 or 2 letters which are closest to
> > the n-th character in the string. Is there a simple regexp to perform
> > that task?
> 
> Well, I wouldn't exactly call this regex simple...  But I have come up with
> one that does it:
... 
> /^(?(?=.{0,$r}[A-Z]).{0,$r}|.*)([A-Z])(?(?<!^..{$r}).*?([A-Z]|$))/

> /^.{$r}([A-Z])/ || /^.{0,$r}([A-Z]).*?([A-Z]|$)/ || /^.*([A-Z])/
 
> /^.{$r}([A-Z])|^.{0,$r}([A-Z]).*?([A-Z]|$)|^.*([A-Z])/

Woh! I'm glad that's complicated, as I don't feel so bad just doing it the
naive way. (I chose the loop, rather than the joining of the result from two
substrs, one using a /^ match and the other using a $/.)

I have another idea how to solve the problem of isolating the (1 or) 2 closest
real letters:

In the A_Z_K_ case, for n=2 (r=1), an equally useful output could be
       A_Z___
That is, to replace anything beyond the first letter at or after char r with
the underscore. Two passes would be necessary, one to strip that which is
before, one to strip that which is after.

Of course, I can probably simplify things by handling the cases of when there
is and is not a letter at the point of interest separately. If there is a
letter there, the answer is already found.

It's not a big deal, I was just noodling...

Thanks for the rather heroic regexps though!
Phil



()  ASCII ribbon campaign      ()    Hopeless ribbon campaign
/\    against HTML mail        /\  against gratuitous bloodshed

[stolen with permission from Daniel B. Cristofani]


       
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. 
Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469

Reply via email to