Ukesh’s Weblog

Regular Expression

Posted in Regular expression by ukesh on September 16, 2010

Only String = /^[a-zA-Z]+$/
ex: “Therock”

Only Digits = /^\d+$/
ex: “3215″

Alphanumeric and underscore = /^\w+$/ or /^[a-zA-Z0-9\_]+$/
ex: address,address_1

Email = /^[\w\.]+@\w+\.[a-z]{1,3}$/
ex: rails@gmail.com, rails_get@gmail.com

Mobile Number(starts with ‘+’ and only 12 digits) = /^\+\s\d{12}$/
ex: +919999988888

Get left side value
ex: “ukesh kumar”.scan(/(.*)\s/).flatten.to_s

Get right side value
ex: “ukesh kumar”.scan(/\s(.*)/).flatten.to_s

Standard Username/login = /^\w[\w\.+\-_@ ]+$/
ex: ukesh86, uk.esh

Get left side value
ex: “ukesh kumar”.s

Get right side value
ex: “ukesh kumar”.s

Standard Username
ex: ukesh86, uk.esh

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.