Regex help

C

Chris Lervag

Hi
I want to test a string for three properties:
1) It is exactly 9 characters long.
2) Character number 5 is a comma (',').
3) The other characters are either numbers 0-9 or letters a-f.

I have been able to do this by a combination of strings methods for 1)
and 2), and using a regex match on the string (stripped of the comma)
with the following code:
myString =~ /([^a-f0-9])/

If possible I would like to simplify this by using a more advanced regex
match to check all three properties at one time. If this is possible,
perhaps one of you regex wizards out there could show me how? :)

Best regards,
Chris
 
S

Stefano Crocco

|Hi
|I want to test a string for three properties:
|1) It is exactly 9 characters long.
|2) Character number 5 is a comma (',').
|3) The other characters are either numbers 0-9 or letters a-f.
|
|I have been able to do this by a combination of strings methods for 1)
|and 2), and using a regex match on the string (stripped of the comma)
|with the following code:
|myString =~ /([^a-f0-9])/
|
|If possible I would like to simplify this by using a more advanced regex
|match to check all three properties at one time. If this is possible,
|perhaps one of you regex wizards out there could show me how? :)
|
|Best regards,
|Chris

You could do this with a regexp which matches the following:
* the beginning of the string
* four characters of type a-f or digits
* a comma
* four characters of type a-f or digits
* the end of the string

The regexp is:
/^[a-f\d]{4},[a-f\d]{4}$/

Note that your string can contain newlines, you must replace ^ and $ with \A
and \Z respectively.

I hope this helps

Stefano
 
J

Jesús Gabriel y Galán

Hi
I want to test a string for three properties:
1) It is exactly 9 characters long.
2) Character number 5 is a comma (',').
3) The other characters are either numbers 0-9 or letters a-f.

I have been able to do this by a combination of strings methods for 1)
and 2), and using a regex match on the string (stripped of the comma)
with the following code:
myString =~ /([^a-f0-9])/

If possible I would like to simplify this by using a more advanced regex
match to check all three properties at one time. If this is possible,
perhaps one of you regex wizards out there could show me how? :)

irb(main):006:0> re = /\A[a-f0-9]{4},[a-f0-9]{4}\z/
=> /\A[a-f0-9]{4},[a-f0-9]{4}\z/
irb(main):007:0> "abcd,4f2ddf" =~ re
=> nil
irb(main):008:0> "abcd,4f32" =~ re
=> 0

Hope this helps,

Jesus.
 
R

Robert Klemme

2010/1/11 Stefano Crocco said:
|Hi
|I want to test a string for three properties:
|1) It is exactly 9 characters long.
|2) Character number 5 is a comma (',').
|3) The other characters are either numbers 0-9 or letters a-f.
|
|I have been able to do this by a combination of strings methods for 1)
|and 2), and using a regex match on the string (stripped of the comma)
|with the following code:
|myString =3D~ /([^a-f0-9])/
|
|If possible I would like to simplify this by using a more advanced rege= x
|match to check all three properties at one time. If this is possible,
|perhaps one of you regex wizards out there could show me how? :)
|
|Best regards,
|Chris

You could do this with a regexp which matches the following:
* the beginning of the string
* four characters of type a-f or digits
* a comma
* four characters of type a-f or digits
* the end of the string

The regexp is:
=A0/^[a-f\d]{4},[a-f\d]{4}$/

Note that your string can contain newlines, you must replace ^ and $ with= \A
and \Z respectively.

I believe this should rather be \z because \Z allows for a follwing
newline. So that would be

/\A[a-f\d]{4},[a-f\d]{4}\z/

see http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Stefano Crocco

|> You could do this with a regexp which matches the following:
|> * the beginning of the string
|> * four characters of type a-f or digits
|> * a comma
|> * four characters of type a-f or digits
|> * the end of the string
|>
|> The regexp is:
|> /^[a-f\d]{4},[a-f\d]{4}$/
|>
|> Note that your string can contain newlines, you must replace ^ and $
|> with \A and \Z respectively.
|
|I believe this should rather be \z because \Z allows for a follwing
|newline. So that would be
|
|/\A[a-f\d]{4},[a-f\d]{4}\z/

You're right.

Stefano
 
C

Chris Lervag

Gavin said:
Slightly more terse:
/\A\h{4},\h{4}\z/
...where \h is a hexadecimal character [0-9a-fA-F] (assuming upper-
case a-f is fine).

Alright, thanks guys for excellent insight and help! I especially like
this last suggestion which is both compact and easy to comprehend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,299
Latest member
JewelDeLaC

Latest Threads

Top