tags:

views:

753

answers:

10

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.

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:

  1. Strip the beginning of a line off of every line in a file that looks like:
    Line 25634 :
    Line 632157 :

  2. 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.

  3. 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.

  4. In a CSV file with no escaped commas, replace the first character of the 8th column of each row with a capital A.

  5. Given a bunch of GDB stack traces with lines like
    #3 0x080a6d61 in _mvl_set_req_done (req=0x82624a4, result=27158) at ../../mvl/src/mvl_serv.c:850
    strip out everything from each line except the function names.

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.

+2  A: 

Just last week, I used regex find/replace to convert a CSV file to an XML file.

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.

Matt Sheppard
+2  A: 

Regex make it easy to replace whole words using word boundaries.

(\b\w+\b)

So you can replace unwanted words in your file without disturbing words like Scunthorpe

Ed Guiness
yes, or Classic. ;o)
Keng
yes, or Clbuttic ;o)
Shahin
+1  A: 

I like to use regexps to reformat lists of items like this:

int item1
double item2

to

public void item1(int item1){
}
public void item2(double item2){
}

This can be a big time saver.

David Schmitt
A: 

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 <-> PHP is the one I do most often - and you don't want to fart around making the same change 500 times.

mabwi
+1  A: 

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!

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.

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.

Keng
A: 

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.

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.

spoon16
A: 

I agree with you on points 3, 4, and 5 but not necessarily points 1 and 2.

In some cases 1 and 2 are easier to achieve using a anonymous keyboard macro.

By this I mean doing the following:

  1. Position the cursor on the first line
  2. Start a keyboard macro recording
  3. Modify the first line
  4. Position the cursor on the next line
  5. Stop record.

Now all that is needed to modify the next line is to repeat the macro.

I could live with out support for regex but could not live without anonymous keyboard macros.

jussij
+1  A: 

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.

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.

+1  A: 

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.

Create Table text:

...
field_1 VARCHAR2(100) NULL,
field_2 VARCHAR2(10) NULL,
field_3 NUMBER(8) NULL,
field_4 VARCHAR2(100) NULL,
....

My Regex Search:

/([a-z_])+ .*?,?/

My Replacement:

pstmt.setString(1, \1);

The result:

...
pstmt.setString(1, field_1);
pstmt.setString(1, field_2);
pstmt.setString(1, field_3);
pstmt.setString(1, field_4);
....

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.

localshred
+1  A: 

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:

/.{20,60} /

...and this replacement:

/$0"¶         + "/

...where the pilcrow is an actual newline, and the number of spaces varies from one incident to the next. Result:

String s = "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.";
Alan Moore