read

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!

Karthik


Published on

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

the dotNet way!

experiments w/ asp.net, c#, ruby and many more!!

Back to Home