[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

yet another regexp problem

Clint

7/3/2008 9:51:00 AM

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
7 Answers

Srijayanth Sridhar

7/3/2008 10:08:00 AM

0

/([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
>
>

Srijayanth Sridhar

7/3/2008 10:11:00 AM

0

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

Clint

7/3/2008 10:22:00 AM

0

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 :(


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.

benjamin.billian@googlemail.com

7/3/2008 10:45:00 AM

0

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 :(

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

David A. Black

7/3/2008 10:48:00 AM

0

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.r... for details and updates!

Clint

7/3/2008 11:29:00 AM

0

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

F. Senault

7/3/2008 11:34:00 AM

0

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.

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
--
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)