# Yet another regexp problem

**URL:** <https://www.ruby-forum.com/t/yet-another-regexp-problem/142095>\
**Category:** Ruby\
**Created:** [July 3, 2008, 11:55am UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095 "2008-07-03T11:55:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 11:55am UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/1 "2008-07-03T11:55:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/2 "2008-07-03T12:11:54Z")

</div>

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

Should work.

Jayanth

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/3 "2008-07-03T12:14:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:50pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/4 "2008-07-03T12:50:39Z")

</div>

On 3 Jul., 12:22, shaman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Srijayanth S. 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 ☹

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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/5 "2008-07-03T12:51:50Z")

</div>

Hi –

On Thu, 3 Jul 2008, shaman wrote:

> 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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/6 "2008-07-03T12:25:58Z")

</div>

Srijayanth S. pisze:

> /([a-z]{3})[02-9][0-9]{2}\1/
> 
> Should work.  
> =\> 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 ☹

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.

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/7 "2008-07-03T12:55:57Z")

</div>

Hi –

On Thu, 3 Jul 2008, Benjamin Billian wrote:

> 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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 1:37pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/8 "2008-07-03T13:37:35Z")

</div>

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

> locale-sensitive. You can also use [A-Za-z] if that’s sufficient.  
> 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

---

<div class="post-metadata">

**Author:** ![shaman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@shaman](https://www.ruby-forum.com/u/shaman)\
**Post date:** [July 3, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/yet-another-regexp-problem/142095/9 "2008-07-03T13:36:24Z")

</div>

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! 🙂

regards-  
shaman
