RegEx Help For checking alphnumeric

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

Is this right.

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

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

From my iPhone

Pat

On Thu, Sep 22, 2011 at 3:43 PM, Adam P. [email protected]
wrote:

Note that \d+\d+ is almost redundant, but you’ll actually not match “v3”.
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 2:35 PM, Saurabh A. [email protected] wrote:

Is this right.

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

Using Rubular is a good way of quickly checking regular expressions:
http://rubular.com/r/0VTmjIcQ7j

Note that \d+\d+ is almost redundant, but you’ll actually not match
“v3”.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs