Regular Expressions


1) To remove Square Brackets in string:


'[1,2,3]'.gsub(/[\A\[\]\z]/,'')

ingredients:
/[ => start tag
]/ => end tag
\A => start of line
\z => end of line
\[, \] => syntax for special characters opening and closing bracket

2)  Replacing multiple  with constant width:

Regullar expression => /(width:300)|(width: 300)/

/-> start

()-> any thing enclose

|-> or sign

/ -> end

'width: 300px'.gsub(/(width:300)|(width: 300)/,'width:900')

Or other way

\s* -> white space '\s' and '*' for 0 or more occurences

'width: 300px'.gsub(/(width:\s*300)/,'width:900')

3) Removing Brackets and anything enclosed in them:

'abcdef [12] jklm [e1]'.gsub(/\[[^\]]*\]/,'')
Advertisements

I am Senior Software Engineer. I love reading, writing, sharing,developing, hiking, movies, trips, mountains, brooks, hills etc.

Tagged with:
Posted in Ruby on Rails

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 )

Google+ photo

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

Connecting to %s

StackOverFlow
Categories
Archives
%d bloggers like this: