<p>I recently discussed editors with a co-worker.  He uses one of the less popular editors and I use another (I won't say which ones since it's not relevant and I want to avoid an editor flame war).  I was saying that I didn't like his editor as much because it doesn't let you do find/replace with regular expressions.</p>

<p>He said he's never wanted to do that, which was surprising since it's something I find myself doing all the time.  However, off the top of my head I wasn't able to come up with more than one or two examples.  Can anyone here offer some examples of times when they've found regex find/replace useful in their editor?  Here's what I've been able to come up with since then as examples of things that I've actually had to do:</p>

<ol>
<li><p>Strip the beginning of a line off of every line in a file that looks like:<br>
<code>Line 25634 :</code><br>
<code>Line 632157 :</code></p></li>
<li><p>Taking a few dozen files with a standard header which is slightly different for each file and stripping the first 19 lines from all of them all at once.</p></li>
<li><p>Piping the result of a MySQL select statement into a text file, then removing all of the formatting junk and reformatting it as a Python dictionary for use in a simple script.</p></li>
<li><p>In a CSV file with no escaped commas, replace the first character of the 8th column of each row with a capital A.</p></li>
<li><p>Given a bunch of GDB stack traces with lines like<br>
<code>#3  0x080a6d61 in _mvl_set_req_done (req=0x82624a4, result=27158) at ../../mvl/src/mvl_serv.c:850</code><br>
strip out everything from each line except the function names.</p></li>
</ol>

<p>Does anyone else have any real-life examples?  The next time this comes up, I'd like to be more prepared to list good examples of why this feature is useful.</p>

[{'answer_id': 51901, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 2, 'selected': False, 'text': "<p>Just last week, I used regex find/replace to convert a CSV file to an XML file.</p>\n\n<p>Simple enough to do really, just chop up each field (luckily it didn't have any escaped commas) and push it back out with the appropriate tags in place of the commas.</p>\n"}, {'answer_id': 51911, 'author': 'Ed Guiness', 'author_id': 4200, 'author_profile': 'https://Stackoverflow.com/users/4200', 'pm_score': 2, 'selected': False, 'text': '<p>Regex make it easy to replace whole words using word boundaries.</p>\n\n<pre><code>(\\b\\w+\\b)\n</code></pre>\n\n<p>So you can replace unwanted words in your file without disturbing words like <strong>Scunthorpe</strong></p>\n'}, {'answer_id': 51918, 'author': 'David Schmitt', 'author_id': 4918, 'author_profile': 'https://Stackoverflow.com/users/4918', 'pm_score': 1, 'selected': False, 'text': '<p>I like to use regexps to reformat lists of items like this:</p>\n\n<pre><code>int item1\ndouble item2\n</code></pre>\n\n<p>to</p>\n\n<pre><code>public void item1(int item1){\n}\npublic void item2(double item2){\n}\n</code></pre>\n\n<p>This can be a big time saver.</p>\n'}, {'answer_id': 51923, 'author': 'MattBelanger', 'author_id': 655, 'author_profile': 'https://Stackoverflow.com/users/655', 'pm_score': 0, 'selected': False, 'text': "<p>The first thing I do with any editor is try to figure out it's Regex oddities.  I use it all the time.  Nothing really crazy, but it's handy when you've got to copy/paste stuff between different types of text - SQL &lt;-> PHP is the one I do most often - and you don't want to fart around making the same change 500 times.</p>\n"}, {'answer_id': 52024, 'author': 'Keng', 'author_id': 730, 'author_profile': 'https://Stackoverflow.com/users/730', 'pm_score': 1, 'selected': False, 'text': "<p>I use it all the time when someone sends me a list of patient visit numbers in a column (say 100-200) and I need them in a '0000000444','000000004445' format.  works wonders for me!</p>\n\n<p>I also use it to pull out email addresses in an email.  I send out group emails often and all the bounced returns come back in one email.  So, I regex to pull them all out and then drop them into a string var to remove from the database.</p>\n\n<p>I even wrote a little dialog prog to apply regex to my clipboard.  It grabs the contents applies the regex and then loads it back into the clipboard.</p>\n"}, {'answer_id': 59755, 'author': 'Eric Schoonover', 'author_id': 3957, 'author_profile': 'https://Stackoverflow.com/users/3957', 'pm_score': 0, 'selected': False, 'text': '<p>Regex is very handy any time I am trying to replace a value that spans multiple lines.  Or when I want to replace a value with something that contains a line break.</p>\n\n<p>I also like that you can match things in a regular expression and not replace the full match using the $# syntax to output the portion of the match you want to maintain.</p>\n'}, {'answer_id': 89322, 'author': 'jussij', 'author_id': 14738, 'author_profile': 'https://Stackoverflow.com/users/14738', 'pm_score': 0, 'selected': False, 'text': '<p>I agree with you on points <strong>3</strong>, <strong>4</strong>, and <strong>5</strong> but not necessarily points <strong>1</strong> and <strong>2</strong>.</p>\n\n<p>In some cases <strong>1</strong> and <strong>2</strong> are easier to achieve using a anonymous keyboard macro.</p>\n\n<p>By this I mean doing the following:</p>\n\n<ol>\n<li>Position the cursor on the first line</li>\n<li>Start a keyboard macro recording</li>\n<li>Modify the first line</li>\n<li>Position the cursor on the next line</li>\n<li>Stop record.</li>\n</ol>\n\n<p>Now all that is needed to modify the next line is to repeat the macro.</p>\n\n<p>I could live with out support for regex but could not live without anonymous keyboard macros.</p>\n'}, {'answer_id': 288835, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>One thing I use it for in web development all the time is stripping some text of its HTML tags. This might need to be done to sanitize user input for security, or for displaying a preview of a news article. For example, if you have an article with lots of HTML tags for formatting, you can\'t just do LEFT(article_text,100) + \'...\' (plus a "read more" link) and render that on a page at the risk of breaking the page by splitting apart an HTML tag.</p>\n\n<p>Also, I\'ve had to strip img tags in database records that link to images that no longer exist. And let\'s not forget web form validation. If you want to make a user has entered a correct email address (syntactically speaking) into a web form this is about the only way of checking it thoroughly.</p>\n'}, {'answer_id': 288859, 'author': 'localshred', 'author_id': 29690, 'author_profile': 'https://Stackoverflow.com/users/29690', 'pm_score': 2, 'selected': False, 'text': "<p>Yesterday I took a create table statement I made for an Oracle table and converted the fields to setString() method calls using JDBC and PreparedStatements. The table's field names were mapped to my class properties, so regex search and replace was the perfect fit.</p>\n\n<p>Create Table text:</p>\n\n<pre><code>...\nfield_1 VARCHAR2(100) NULL,\nfield_2 VARCHAR2(10) NULL,\nfield_3 NUMBER(8) NULL,\nfield_4 VARCHAR2(100) NULL,\n....\n</code></pre>\n\n<p>My Regex Search:</p>\n\n<pre><code>/([a-z_])+ .*?,?/\n</code></pre>\n\n<p>My Replacement:</p>\n\n<pre><code>pstmt.setString(1, \\1);\n</code></pre>\n\n<p>The result:</p>\n\n<pre><code>...\npstmt.setString(1, field_1);\npstmt.setString(1, field_2);\npstmt.setString(1, field_3);\npstmt.setString(1, field_4);\n....\n</code></pre>\n\n<p>I then went through and manually set the position int for each call and changed the method to setInt() (and others) where necessary, but that worked handy for me. I actually used it three or four times for similar field to method call conversions.</p>\n"}, {'answer_id': 1374860, 'author': 'Alan Moore', 'author_id': 20938, 'author_profile': 'https://Stackoverflow.com/users/20938', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve just pasted a long character sequence into a string literal, and now I want to break it up into a concatenation of shorter string literals so it doesn\'t wrap.  I also want it to be readable, so I want to break only after spaces.  I select the whole string (minus the quotation marks) and do an in-selection-only replace-all with this regex:</p>\n\n<pre><code>/.{20,60} /\n</code></pre>\n\n<p>...and this replacement:</p>\n\n<pre><code>/$0"¶         + "/\n</code></pre>\n\n<p>...where the pilcrow is an actual newline, and the number of spaces varies from one incident to the next.  Result:</p>\n\n<pre><code>String s = "I recently discussed editors with a co-worker. He uses one "\n         + "of the less popular editors and I use another (I won\'t say "\n         + "which ones since it\'s not relevant and I want to avoid an "\n         + "editor flame war). I was saying that I didn\'t like his "\n         + "editor as much because it doesn\'t let you do find/replace "\n         + "with regular expressions.";\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51884', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1694/']