Regular expression question

As I don’t have my copy of Mastering Regular Expressions at home, and
I’m not that good with them (yet :slight_smile: I question for the RegEx gurus:

if I want to match “dec” at the beginning of a string how would I do
that:

/\Adec/

matches on string “decimal” and “dec” basically I want ‘dec’ not
followed by [A-Za-z0-9_-] but anything else is ok (including ‘dec’ being
the entire string)

Thanks in advance

-rich

sorry for answering my own question…just found

/\Adec(?![a-zA-Z0-9_])/

-rich

···

-----Original Message-----
From: Rich Kilmer [mailto:rich@infoether.com]
Sent: Saturday, September 07, 2002 5:15 PM
To: ruby-talk ML
Subject: Regular expression question

As I don’t have my copy of Mastering Regular Expressions at
home, and I’m not that good with them (yet :slight_smile: I question for
the RegEx gurus:

if I want to match “dec” at the beginning of a string how would I do
that:

/\Adec/

matches on string “decimal” and “dec” basically I want 'dec’
not followed by [A-Za-z0-9_-] but anything else is ok
(including ‘dec’ being the entire string)

Thanks in advance

-rich

Hi –

As I don’t have my copy of Mastering Regular Expressions at home, and
I’m not that good with them (yet :slight_smile: I question for the RegEx gurus:

Answering questions addressed to gurus is usually a guarantee that
I’ll get it wrong, but I’ll try anyway :slight_smile:

if I want to match “dec” at the beginning of a string how would I do
that:

/\Adec/

matches on string “decimal” and “dec” basically I want ‘dec’ not
followed by [A-Za-z0-9_-] but anything else is ok (including ‘dec’ being
the entire string)

You want a negative lookahead:

/\Adec(?![A-Za-z0-9_-])/

You can shorten it to:

/\Adec(?![\w-])/

the difference being that \w is locale-dependent (which is quite
likely good, if you want to avoid all alphanumeric characters).

David

···

On Sun, 8 Sep 2002, Rich Kilmer wrote:


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

Actually I did not need the - so the /\Adec(?[\w])/ worked great.

Thank you my RegEx guru :wink:

-rich

···

-----Original Message-----
From: dblack@candle.superlink.net
[mailto:dblack@candle.superlink.net]
Sent: Saturday, September 07, 2002 5:24 PM
To: ruby-talk ML
Subject: Re: Regular expression question

Hi –

On Sun, 8 Sep 2002, Rich Kilmer wrote:

As I don’t have my copy of Mastering Regular Expressions at
home, and
I’m not that good with them (yet :slight_smile: I question for the RegEx gurus:

Answering questions addressed to gurus is usually a guarantee
that I’ll get it wrong, but I’ll try anyway :slight_smile:

if I want to match “dec” at the beginning of a string how would I do
that:

/\Adec/

matches on string “decimal” and “dec” basically I want ‘dec’ not
followed by [A-Za-z0-9_-] but anything else is ok (including 'dec’
being the entire string)

You want a negative lookahead:

/\Adec(?![A-Za-z0-9_-])/

You can shorten it to:

/\Adec(?![\w-])/

the difference being that \w is locale-dependent (which is
quite likely good, if you want to avoid all alphanumeric characters).

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

Hi –

···

On Sun, 8 Sep 2002, Rich Kilmer wrote:

Actually I did not need the - so the /\Adec(?[\w])/ worked great.

In that case you don’t need the […], because \w creates an implicit
character class of its own. (But you do need to restore the ‘!’ :slight_smile:

/\Adec(?!\w)/

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

dblack@candle.superlink.net writes:

Actually I did not need the - so the /\Adec(?[\w])/ worked great.

In that case you don’t need the […], because \w creates an implicit
character class of its own. (But you do need to restore the ‘!’ :slight_smile:

/\Adec(?!\w)/

I think you could write this

/\Adec\b/

where \b matches the “word boundary” after “dec”.

/Johan Holmberg

···

On Sun, 8 Sep 2002, Rich Kilmer wrote:

Switched to that…it works great.

Thanks,

rich

···

-----Original Message-----
From: Johan Holmberg [mailto:holmberg@iar.se]
Sent: Sunday, September 08, 2002 3:10 PM
To: ruby-talk ML
Subject: Re: Regular expression question

dblack@candle.superlink.net writes:

On Sun, 8 Sep 2002, Rich Kilmer wrote:

Actually I did not need the - so the /\Adec(?[\w])/ worked great.

In that case you don’t need the […], because \w creates
an implicit
character class of its own. (But you do need to restore
the ‘!’ :slight_smile:

/\Adec(?!\w)/

I think you could write this

/\Adec\b/

where \b matches the “word boundary” after “dec”.

/Johan Holmberg