############# EMACS REGULAR EXPRESSIONS 11 January 2003 20 August 2004 - separated from emacs help For extended help, C-h i m emacs m regexps \( \) to tag \| for or .* for lots of unspecified text (no new-lines) .*? for "not greedy" unspecified text f+ means match f or ff or fff etc. ++ means match + or ++ or +++, etc. don't backslash [ or ] in replacement reg. exp. \w for characters that should be used in words ('alphanumeric', but more extended) [0-9] for numbers from 0 to 9 --greedy vs. not greedy--- given +---+---+ this ++[-+]+\+ matches this +---+---+ but this ++[-+]+?\+ matches this +---+ ------------------------------------- ----Reg. Exp. replacement--- C-M-% (i.e. C-M--5) to do a replacement via reg. exp. that prompts for each replacement, but you can type ! to replace the rest. Ones I like to use: ------ I use these two commands to replace kana ++KANJI[kana] with KANJI in HTML (Note this was written in Times New Roman, Regular, 10pt. font to make spaces and tabs line up.) To replace [ruby text] with (ruby text) that will be above words: ( This requires an additional replacement) \[\([^]]*\)\] replace with (\1) To wrap the tag around a word and it's ruby text, as modified by the above replacement: ( Note the use of non-greedy replacement. The word should have ++ written before it.) ++\(.*?)\) replace with \1 OR with just one command ++\(.*?\)\[\(.*?\)\] replace with \1(\2) NOTE: don't mistake m for [ the regexp will not find it. Try this: copy the second [ and paste it into the find box. it will not find the first one. --------------To convert assignments to a printout of members of a struct (C code) 25 March 2003 \(dirEnt\..+?\) = root\[.+?\]; replace with fprintf(stdout,"/1%d",/1); -------------- To move something labeled later in the file, where the thing labeled is at the end of a line, and the line is the last so labeled in the file --- e.g. Replace---- {A:} yadayada ... {A:} dataya asdf aje keyword ------------ --- With ---- yadayada ... {A:} dataya asdf aje keyword ------------- {\(.*\)}\([^@]*\)\1\(.*?\)\(\S-*\) (with a C-q C-j to add a newline to the search) replace with <\4>\2\1\3\4 (with a C-q C-j to add a newline to the end of the search string. This is needed) NOTE: make sure you never use @ in your file. ---- Convert // comments to /* */ comments //\(.*$\) replace with: /* \1 */ ---- Similarly, I used this for the lowercase letters: \(.*\):{\([^@]*\)\1|\(.*?\)\(\S-*\) (with C-q C-j ending) replace with <\4~>\2\1\3\4 (with C-q C-j ending) NOTE: make sure you never use @ in your file. --- Yet another one --- (Type C-q C-j where it says ) [ ]*\([A-Z]\)\.\([^@]*{\)\1\(:}.*?\)\([^ ]*\) replace with <\4>\2\1\3\4 NOTE: make sure you never use @ in your file. ------------ To get rid of microsoft s \(.*?\) replace with \1 --- Two character words ending in @-- ^.[‚ʂӂނ䂁め] <-Including the space, but not this arrow OR, to find the Ђ炁: \[[‚ʂӂނ䂁め]\] ----- HTML index link-maker ----- 19 Aug 2003 + First, extract the headers: copy to a seperate buffer and replace \([^@]*?\)\(.*\) with \2 + Convert headings to a linked list and edit to your satisfaction: replace h.> with li> and then insert
    tags to create the proper nesting. Oh, yes. You really should number your tags, but it's not necessary, I hope. + Then, make the links! FOR LINKS NAMES AFTER COMPLETE TITLE: replace \(
  • \)\(.*\)\(
  • \)\([^@]*?\)\(\)\2\(.*?\)\(\) with \1\2\3\4\5\2\6\7 FOR 2-DIGITS, decimal seperated link names replace \(
  • \)\([0-9].[0-9]?\)\(.*\)\(
  • [^@]*?\)\(\)\2\(.*?\)\(\) with \1\2\3\4\5\2\6\7 if you replace with this, it's the same: \1\2\3\4\5\2\6\7 NOTE: make sure you never use @ in your file. --get/set creater-- Warning: I'm updating this without checking it. Create a line like private String MyName; /*** Much stuff **/ } and this will give you private String MyName; /*** Much stuff **/ public String get$MyName(){ return MyName; } public set$MyName(String MyName){ this.MyName=MyName; } } The dollars are so you can do the capitalization right on the methods, manually for now. REPLACE \(private[ ]*\)\([^ ]*\)\([ ]*\)\(.*\)\(;[^@]*?\)\( }\) WITH \1\2\3\4\5 public \2 get$\4(){ return \4; } public void set$\4(\2 \4){ this.\4=\4; }\6 (with newlines between each line above (C-q C-c = \n)) ---- table of contents to headers ---- replace ^
  • \(.*$\) with

    \1

    Edit the number of spaces after the ^ and the digits (3 in the example) according to the level of the document you are creating the headers for. -- split declaration from first usage -- EXAMPLE REPLACE int x = this.getX(); - STUFF - } -MORESTUFF:$$^- { WITH int x = this.getX(); - STUFF - int x; } -MORESTUFF:$$^- x = this.getX(); -{ EXPRESSION REPLACE \([^ ]*\)\([ ]*\)\([^ ]*\)\([ ]*=[ ]*\)\([^ ]*\)\([^@]*\)\( }\)\([^@]*\) \({\) WITH \1\2\3\4\5\6 \1\2\3;\7\8 \3\4\5\9 (with newlines between each line above (C-q C-c = \n)) --- Get the right number of spaces at the end of sentences -- REPLACE \. \([^ ]\) WITH . \1 (This could use some more work to make it not snag on things like "Dr. XXX") --- Convert banner WEB PAY records to a reasonable form REPLACE \(.*?\) \(.*?\) \(.*?\) \(.*\) .* .* \(.*\) .*$ WITH \1 \2 \3\4 \5 -- Convert banner WEB time records -- REPLACE ^.*? \(.*?\) .*? .*? \(.*?\) .*? .*? \(.*\) WITH \1 \2 \3 -- Convert YY/MM/DD to MM/DD/YY (Date format) REPLACE ^\([0-9]\{2,2\}\)/\([0-9]\{1,2\}\)/\([0-9]\{1,2\}\) WITH \2/\3/\1 --- The next few are used in auto-html creation of picture-pages ~~~~ Turn figures into numbered images (matlab commands) NOTE: figure and thing that displays image must be on same line. REPLACE ^\(figure(\)\(.\)\().*(\)\(.*?\)\().*?\)$ WITH \1\2\3\4\5 imwrite('fig\2-\4.jpg'); ~~~~ create HTML table (with added descriptions) of images NOTE: image name must not contain spaces. REPLACE ^\(.*?\) \(.*\)$ WITH \2 --- Eliminate repeated lines that start with the same word --- REPLACE ^\(\([^ ]*\).*\)\( \1.*\)* WITH \1 --- Bad character (JUNK) replacement --- f WITH ' g WITH " h WITH " c WITH ... WITH ' WITH – --- Extra digit replacement --- trim 1.233333333 to 1.23 REPLACE \(\.[0-9]\{1,2\}\)[0-9]* WITH \1