[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Negative lookahead in Regexp question

Axel Etzold

6/16/2007 3:23:00 PM

Dear all,

I have strings like this one:

"<NC> In North Carolina </NC>"

and I'd like to match the part in between the brackets with
a regexp with negative lookahead excluding a substring,
(</NC> in this case, rather than just single characters),
but I can't get it right...


Thanks for your help,

Axel


--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/mult...

5 Answers

byronsalty

6/16/2007 4:13:00 PM

0

On Jun 16, 11:22 am, "Axel Etzold" <AEtz...@gmx.de> wrote:
> Dear all,
>
> I have strings like this one:
>
> "<NC> In North Carolina </NC>"
>
> and I'd like to match the part in between the brackets with
> a regexp with negative lookahead excluding a substring,
> (</NC> in this case, rather than just single characters),
> but I can't get it right...
>
> Thanks for your help,
>
> Axel
>
> --
> Psssst! Schon vom neuen GMX MultiMessenger gehört?
> Der kanns mit allen:http://www.gmx.net/de/go/mult...

Can you provide a few examples of what you're looking for? I'm not
sure what you're asking but it doesn't sound too bad.

- Byron
-----
http://www.salt...

Axel Etzold

6/16/2007 6:41:00 PM

0

Dear Byron,

thank you for responding.
I am working on an analysis of so-called chunked text,
i.e., an analysis of words in a sentence, that
classifies words as nouns / verbs / adjectives etc.

A typical sentence with chunking tags thus looks like this:

"<NC> The physical descriptions </NC> <PC> of <NC> places </NC> <PC> in <NC> North Carolina </NC> </PC> , <PC> in </PC> <ADV> so far </ADV> as <NC> they </NC> <VC> are </VC> <NC> specific </NC> <PC> at <NC> all </NC> </PC> , <VC> owe </VC> <NC> a little </NC> <PC> to <NC> memories </NC> </PC> <PC> of <NC> my childhood </NC>, although <NC> I </NC> <VC> 've also borrowed </VC> <ADV> indiscriminately </ADV> <PC> from <NC> other people 's childhood memories </NC> </PC> <PC> as </PC> <ADV> well </ADV> ."

Originally, I wanted to use Regexps to split the original sentence
into groups using negative lookahead, which I've now skipped in favor
of repeated Array.splits, but I think I could you use knowing how to
search for a substring using negative lookahead, i.e., as in my example:

regexp=/.../ <= searched for, such that:
string="<NC> In North Carolina </NC>"
ref=regexp.match(string)
p ref[1] => "In North Carolina"

Thank you for any help!

Best regards,

Axel
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/mult...

Axel Etzold

6/16/2007 7:21:00 PM

0

Aureliano,

no - since the tags are not XML tags, and since
I wanted to know about negative lookahead
for regexps ...

Best regards,

Axel
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/...

byronsalty

6/16/2007 7:52:00 PM

0

On Jun 16, 2:40 pm, "Axel Etzold" <AEtz...@gmx.de> wrote:
> regexp=/.../ <= searched for, such that:
> string="<NC> In North Carolina </NC>"
> ref=regexp.match(string)
> p ref[1] => "In North Carolina"

This will work pretty well (works for the above):
/<\w+>(.*?)<\/\w+>/

The only thing fancy there is making the .* non-greedy by adding .*?.
This means it will take the shortest possible match instead of the
longest.

But it will not work as I think you would want with a string of nested
clauses. If you want to include internal clauses then you would need
to make sure that the close tag matches the open tag. The side effect
is that you'll need to have another sub match within the regex.

So consider:
/<(\w+)>(.*?)<\/\1>/

Example:
irb(main):033:0> str = "<NC>In North Carolina <FOO>adsf</FOO> </NC>"
=> "<NC>In North Carolina <FOO>adsf</FOO> </NC>"
irb(main):034:0> re = /<(\w+)>(.*?)<\/\1>/
=> /<(\w+)>(.*?)<\/\1>/
irb(main):035:0> re.match(str)[1]
=> "NC"
irb(main):036:0> re.match(str)[2]
=> "In North Carolina <FOO>adsf</FOO> "


Does that help?

Axel Etzold

6/16/2007 8:50:00 PM

0

Dear Byron,

> Does that help?

Yes, very much ! Thank you for your time!

Best regards,

Axel

--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/s...