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
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
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 3 Jul., 12:22, shaman [email protected] 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}/
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
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.
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
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
David A. Black pisze:
Hi –
xxx765xxx should match
vvv123vvv should be omittedTry this: /[[:alpha]]{3}(?!123)\d{3}[[:alpha:]]{3}/
[cut]
it works! thanks!
regards-
shaman
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs