Yet another regexp problem

Hello-

I need to match any string which contain:

any 3 letters
any 3 digits _but_not_123_
any 3 letters

so:
xxx765xxx should match
vvv123vvv should be omitted

is it possible to write proper regular expression?

Regards-
shaman

/([a-z]{3})[02-9][0-9]{2}\1/

Should work.

Jayanth

···

On Thu, Jul 3, 2008 at 3:21 PM, shaman <noone@nowhere.com> wrote:

Hello-

I need to match any string which contain:

any 3 letters
any 3 digits _but_not_123_
any 3 letters

so:
xxx765xxx should match
vvv123vvv should be omitted

is it possible to write proper regular expression?

Regards-
shaman

Hi --

···

On Thu, 3 Jul 2008, shaman wrote:

Hello-

I need to match any string which contain:

any 3 letters
any 3 digits _but_not_123_
any 3 letters

so:
xxx765xxx should match
vvv123vvv should be omitted

Try this: /[[:alpha]]{3}(?!123)\d{3}[[:alpha:]]{3}/

It uses negative lookahead. The \d{3} part only matches if it occurs
at a point where the next thing in the string is not "123".

I'm also using the POSIX "alpha" character class, which should be
locale-sensitive. You can also use [A-Za-z] if that's sufficient.

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!

irb(main):008:0> m
=> /([a-z]{3})[02-9][0-9]{2}\1/
irb(main):009:0> a='xxx765xxx'
=> "xxx765xxx"
irb(main):010:0> b='vvv123vvv'
=> "vvv123vvv"
irb(main):011:0> a.match(m)
=> #<MatchData:0xb7dc0354>
irb(main):012:0> puts a.match(m)
xxx765xxx
=> nil
irb(main):013:0> puts b.match(m)
nil
=> nil

Jayanth

···

On Thu, Jul 3, 2008 at 3:41 PM, Srijayanth Sridhar <srijayanth@gmail.com> wrote:

/([a-z]{3})[02-9][0-9]{2}\1/

Should work.

Jayanth

On Thu, Jul 3, 2008 at 3:21 PM, shaman <noone@nowhere.com> wrote:

Hello-

I need to match any string which contain:

any 3 letters
any 3 digits _but_not_123_
any 3 letters

so:
xxx765xxx should match
vvv123vvv should be omitted

is it possible to write proper regular expression?

Regards-
shaman

David A. Black pisze:

Hi --

xxx765xxx should match
vvv123vvv should be omitted

Try this: /[[:alpha]]{3}(?!123)\d{3}[[:alpha:]]{3}/

[cut]

it works! thanks! :slight_smile:

regards-
shaman

Note that, sometimes, it's easier / more efficient to use RegExpen to
find slightly too many matches and then filter them afterwards.

Something like :

"xxx765xxx vvv123vvv zzz555ooo".\
  scan(/([A-Za-z]{3}([0-9]{3})[A-Za-z]{3})/) do |all, num|
    puts all unless num == "123"
  end

Fred

···

Le 03 juillet à 12:48, David A. Black a écrit :

so:
xxx765xxx should match
vvv123vvv should be omitted

Try this: /[[:alpha]]{3}(?!123)\d{3}[[:alpha:]]{3}/

It uses negative lookahead. The \d{3} part only matches if it occurs
at a point where the next thing in the string is not "123".

I'm also using the POSIX "alpha" character class, which should be
locale-sensitive. You can also use [A-Za-z] if that's sufficient.

--
When you like music more than life, something's wrong
When you start sleeping as you drive, something's wrong
When you're favorite drink is thinner, something's wrong
When you're proud to be a sinner... (K's Choice, Something's wrong)

Srijayanth Sridhar pisze:

> /([a-z]{3})[02-9][0-9]{2}\1/
>
> Should work.

irb(main):008:0> m
=> /([a-z]{3})[02-9][0-9]{2}\1/
irb(main):009:0> a='xxx765xxx'
=> "xxx765xxx"
irb(main):010:0> b='vvv123vvv'
=> "vvv123vvv"
irb(main):011:0> a.match(m)
=> #<MatchData:0xb7dc0354>
irb(main):012:0> puts a.match(m)
xxx765xxx
=> nil
irb(main):013:0> puts b.match(m)
nil
=> nil

it's not so easy because "vvv133vvv" also should match but your expression return nil :frowning:

irb(main):001:0> reg = /([a-z]{3})[02-9][0-9]{2}\1/
=> /([a-z]{3})[02-9][0-9]{2}\1/
irb(main):002:0> a = "vvv123xxx"
=> "vvv123xxx"
irb(main):003:0> puts a.match(reg)
nil
=> nil
irb(main):004:0> b = "vvv133xxx"
=> "vvv133xxx"
irb(main):005:0> puts b.match(reg)
nil
=> nil

numer 123 is the only one should not match.

The following Expression worked for me:
/[a-z]{3}(?:(?:[^1]\d{2})|(?:1[^2]\d)|(?:12[^3]))[a-z]{3}/

···

On 3 Jul., 12:22, shaman <no...@nowhere.com> wrote:

Srijayanth Sridhar pisze:

> /([a-z]{3})[02-9][0-9]{2}\1/
>
> Should work.

it's not so easy because "vvv133vvv" also should match but your
expression return nil :frowning:

Hi --

···

On Thu, 3 Jul 2008, Benjamin Billian wrote:

On 3 Jul., 12:22, shaman <no...@nowhere.com> wrote:

Srijayanth Sridhar pisze:

> /([a-z]{3})[02-9][0-9]{2}\1/
>
> Should work.

it's not so easy because "vvv133vvv" also should match but your
expression return nil :frowning:

The following Expression worked for me:
/[a-z]{3}(?:(?:[^1]\d{2})|(?:1[^2]\d)|(?:12[^3]))[a-z]{3}/

You can just use negative lookahead:

   (?!123)

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails July 21-24 Edison, NJ
   Advancing With Rails August 18-21 Edison, NJ
See http://www.rubypal.com for details and updates!