EditPlus: Search & Replace with RegEx

Hello people! It’s been so long since I posted anything here. So here you go! This is going to be a short and sweet post! One of my favorite text editor is EditPlus and this is a way by which you could do a search using a RegEx and also replace it by using RegEx! Here is the scenario. I have an XML file where there are some elements without the end tag. So I wanted to replace them at a single shot instead of doing it numerous times. Remember, this is a 50,000 line file, so you can imagine! Here is the text:

<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO">
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO">
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO">
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO">
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO">

The expected result is (self-closing):

<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO"/>
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO"/>
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO"/>
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO"/>
<SomeElement Code="C198440" StartDateTime="01-04-2014 08:02:00.000" EndDateTime="" Admin="PO"/>

You could use the following process in EditPlus to achieve this!

  • Open the “Replace” dialog by pressing Ctrl + H
  • Enter (<SomeElement)([a-zA-Z0-9 \"=\-\:\.]*)(>) in the “Find what” text box
  • Enter \1\2/> in the “Replace with” text box
  • Press the “Replace All” button! That’s it!!

Pretty cool, isn’t it! Made my life so much easier :) And of course, the regex in the find text box can be simplified, but for now it’s fine! Here is what’s happening… The text entered in “Find what” has 3 groups: first group finds a literal “<SomeElement”, second group matches anything that follows it and the third group matches a literal “>”! The text entered in “Replace with” means this: \1 represents the first matched group, used as it is. \2 is the second matched group, also used as it is. Finally a literal “/>” to get the desired XML! Hope you had fun learning something interesting about EditPlus!

Share

Hello World!

Ah, at last! I have a word press site!! I have been thinking about creating a blog software myself using CakePHP. Because my hosting plan initially didn”t support word press installations. But, suddenly I started getting the option for installing other applications and I immediately decided to join the bandwagon! I intend this blog to be dominantly technical. So lets get started! Watch out for my posts! Welcome again!

Share