Wednesday, October 14, 2009

Grouping: A Cool function from Regular Expression

Most of them are very comfortable in find/replacing text in any Text editor. I will be really helpful if you have the exact phrase which you need to replace. Like In the below text if you want to replace word “passionsoft” to “Microsoft” you can easy do that with single find and replace command.

How about if you want to add some text say “Yousuff” at the end of all the word which started with the string passion.

Sample TEXT: was moved to has lot of post related java language. This is all related to passion. Passion is what matters than success and failure.

Here we can’t use direct find/replace, so how do that? I was solving this previously by going manually to all the word which has passion and start appending at the end.. Which I really HATE to do  i.e if we have a text called “passion4java,passionsoft,passion!@#”, all has different value but starts with the string “passion”… I should not loose the text which are after the word passion and it is different for every word… So go about this??

I believing there should be some way where you can overcome this using regular expression, later I found the Grouping command using regular expression which will group the pattern and word which are inside the pattern can be reused


Find text: passion([0-9A-Za-z]*)

Replace text passion$1#--Yousuff--#

It reduced most of my manual text changes , and I thought of share same with all..

Please leave your comments if you find anything interesting :)

No comments:

Post a Comment


This content comes from a hidden element on this page.

The inline option preserves bound JavaScript events and changes, and it puts the content back where it came from when it is closed.
Click me, it will be preserved!

If you try to open a new ColorBox while it is already open, it will update itself with the new content.

Updating Content Example:
Click here to load new content