# RegEx Help For checking alphnumeric

**URL:** <https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666>\
**Category:** ruby-talk\
**Created:** [22 September 2011 12:57 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666 "2011-09-22T12:57:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saurabh\_A](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@Saurabh\_A](https://rubytalk.org/u/Saurabh_A)\
**Post date:** [22 September 2011 12:57 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666/1 "2011-09-22T12:57:54Z")

</div>

Hi,

str = "v38"

I want to check in string using regular expression that 1st character is  
albhabet and rest following is number. As i new to regular expression,  
please help me.

Thanks  
Saurabh

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Saurabh\_A](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@Saurabh\_A](https://rubytalk.org/u/Saurabh_A)\
**Post date:** [22 September 2011 13:35 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666/2 "2011-09-22T13:35:04Z")

</div>

Is this right.

str.match(/(\w)(\d+)(\d+)/)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pat\_Podenski](https://avatars.discourse-cdn.com/v4/letter/p/4491bb/32.png) [@Pat\_Podenski](https://rubytalk.org/u/Pat_Podenski)\
**Post date:** [22 September 2011 13:40 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666/3 "2011-09-22T13:40:59Z")

</div>

Try using rubular.com to verify your regular expression. This a great tool to help with developing regular expressions.

> **···**
>
> From my iPhone  
> ---  
> Pat
> 
> On Sep 22, 2011, at 6:35 AM, "Saurabh A." \<getsauin@gmail.com\> wrote:
> 
> > Is this right.
> > 
> > str.match(/(\w)(\d+)(\d+)/)
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Adam\_Prescott](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@Adam\_Prescott](https://rubytalk.org/u/Adam_Prescott)\
**Post date:** [22 September 2011 13:43 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666/4 "2011-09-22T13:43:25Z")

</div>

Using Rubular is a good way of quickly checking regular expressions:

> **[Rubular](https://rubular.com/r/0VTmjIcQ7j)**
>
> Ruby-based regular expression editor/tester

Note that \d+\d+ is almost redundant, but you'll actually not match "v3".

> **···**
>
> On Thu, Sep 22, 2011 at 2:35 PM, Saurabh A. \<getsauin@gmail.com\> wrote:
> 
> > Is this right.
> > 
> > str.match(/(\w)(\d+)(\d+)/)

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 September 2011 13:56 UTC](https://rubytalk.org/t/regex-help-for-checking-alphnumeric/63666/5 "2011-09-22T13:56:06Z")

</div>

Folks, you forgot anchoring and also that \w matches more than just characters:

irb(main):006:0\> /\w+\d+/ =~ '++++\_0344'  
=\> 4  
irb(main):007:0\> /\w+\d+/ =~ '\_1'  
=\> 0

So better match (!) for the criterion given is

/\A[a-z]\d+\z/i =~ s  
/\A\p{Alpha}\d+\z/ =~ s # on 1.9 and on

Saurabh, I recommend the book "Mastering Regular Expressions". There  
are also quite a few tutorials on the web.

Kind regards

robert

> **···**
>
> On Thu, Sep 22, 2011 at 3:43 PM, Adam Prescott \<adam@aprescott.com\> wrote:
> 
> > On Thu, Sep 22, 2011 at 2:35 PM, Saurabh A. \<getsauin@gmail.com\> wrote:
> > 
> > > Is this right.
> > > 
> > > str.match(/(\w)(\d+)(\d+)/)
> > 
> > Using Rubular is a good way of quickly checking regular expressions:  
> > [Rubular: (\w)(\d+)(\d+)](http://rubular.com/r/0VTmjIcQ7j)
> > 
> > Note that \d+\d+ is almost redundant, but you'll actually not match "v3".
> 
> --  
> remember.guy do |as, often| as.you\_can - without end  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
