# Regex help

**URL:** <https://www.ruby-forum.com/t/regex-help/181397>\
**Category:** Ruby\
**Created:** [January 11, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/regex-help/181397 "2010-01-11T13:39:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/regex-help/181397/1 "2010-01-11T13:39:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 1:51pm UTC](https://www.ruby-forum.com/t/regex-help/181397/2 "2010-01-11T13:51:21Z")

</div>

On Monday 11 January 2010, Chris L. wrote:

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 1:56pm UTC](https://www.ruby-forum.com/t/regex-help/181397/3 "2010-01-11T13:56:02Z")

</div>

On Mon, Jan 11, 2010 at 1:39 PM, Chris L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 1:58pm UTC](https://www.ruby-forum.com/t/regex-help/181397/4 "2010-01-11T13:58:58Z")

</div>

2010/1/11 Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > |myString =~ /([^a-f0-9])/
> 
> - 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/

see [サービス終了のお知らせ](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/regex-help/181397/5 "2010-01-11T14:03:39Z")

</div>

On Monday 11 January 2010, Robert K. wrote:

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 2:45pm UTC](https://www.ruby-forum.com/t/regex-help/181397/6 "2010-01-11T14:45:48Z")

</div>

On Jan 11, 5:57 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > On Monday 11 January 2010, Chris L. wrote:
> > 
> > > |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.  
> > > /\A[a-f\d]{4},[a-f\d]{4}\z/

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/regex-help/181397/7 "2010-01-11T15:44:29Z")

</div>

Gavin K. wrote:

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

---

<div class="post-metadata">

**Author:** ![chrislo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5e9695/32.png) [@chrislo](https://www.ruby-forum.com/u/chrislo)\
**Post date:** [January 11, 2010, 4:35pm UTC](https://www.ruby-forum.com/t/regex-help/181397/8 "2010-01-11T16:35:18Z")

</div>

At 2010-01-11 07:57AM, “Robert K.” wrote:

> /\A[a-f\d]{4},[a-f\d]{4}\z/  
> ON the other hand, \Z saves you a .chomp

> see [サービス終了のお知らせ](http://www.geocities.jp/kosako3/oniguruma/doc/RE.txt)

I’d love to see that kind of documentation in the core Ruby docs.
