Emacs.RegularExpressions History

Hide minor edits - Show changes to output - Cancel

 
 
February 26, 2014, at 04:12 PM by MichaelPaulukonis -
Added lines 66-70:

This uses the [@replace-regexp@] evaluation-feature introduced in Emacs 22, which I read about waaaay back when in Steve Yegge's [[http://steve-yegge.blogspot.com/2006/06/shiny-and-new-emacs-22.html|Shiny and New: Emacs 22]], and never seriously implemented before today. And I'm Emacs 24. O tempora o mores!

There are a lot more examples at [[EmacsWiki:ReplaceRegexp]]

 
 
February 26, 2014, at 04:06 PM by MichaelPaulukonis -
Added lines 63-130:
!!! convert XML tags to UPPERCASE
I know there's a better way of handling underscores.
But this here is today's dirty-laundry....
(:source lang=text:)
\,(upcase \1)
\(</?\w+_?\w+?_?\w+?>\)
(:sourceend:)

(:source lang=xml:)
<locate>
<request>
<id>1</id>
</request>
<response>
<constituent>
<id>1</id>
<mailing_name>Mr. Test Name</mailing_name>
<alpha_sort_name>NAME TEST</alpha_sort_name>
<address>
<addr_sub_type>NADR</addr_sub_type>
<line_1_text>123 Main St.</line_1_text>
<line_2_text></line_2_text>
<line_3_text></line_3_text>
<line_4_text></line_4_text>
<city_name>Great Town</city_name>
<stae_code>MD</stae_code>
<prov_code></prov_code>
<zip_code>99999</zip_code>
<intl_postal_code></intl_postal_code>
<cntr_code>USA</cntr_code>
</address>
</constituent>
<status>0</status>
<error>No Error</error>
</response>
</locate>
(:sourceend:)

(:source lang=xml:)

<LOCATE>
<REQUEST>
<id>1</id>
</REQUEST>
<RESPONSE>
<CONSTITUENT>
<id>1</id>
<MAILING_NAME>Mr. Test Name</MAILING_NAME>
<ALPHA_SORT_NAME>NAME TEST</ALPHA_SORT_NAME>
<ADDRESS>
<ADDR_SUB_TYPE>NADR</ADDR_SUB_TYPE>
<LINE_1_TEXT>123 Main St.</LINE_1_TEXT>
<LINE_2_TEXT></LINE_2_TEXT>
<LINE_3_TEXT></LINE_3_TEXT>
<LINE_4_TEXT></LINE_4_TEXT>
<CITY_NAME>Great Town</CITY_NAME>
<STAE_CODE>MD</STAE_CODE>
<PROV_CODE></PROV_CODE>
<ZIP_CODE>99999</ZIP_CODE>
<INTL_POSTAL_CODE></INTL_POSTAL_CODE>
<CNTR_CODE>USA</CNTR_CODE>
</ADDRESS>
</CONSTITUENT>
<STATUS>0</STATUS>
<ERROR>No Error</ERROR>
</RESPONSE>
</LOCATE>
(:sourceend:)
 
 
January 31, 2014, at 11:24 AM by OtherMichael - the reality was very different
Added lines 40-48:
!!! using regexes to keep/remove matching lines
Sometimes, it just takes too long to find the right regex.
It would probably pay-off in the long-run to get it right, but deadlines are deadlines.
In these cases, I replace the offending party with something unique -- say [@##-##@].
Match on that as required, then replace with the original value.
This wouldn't work for variations.

Specific case -- I was trying to remove all lines containing a period. No matter what I did, passing in a regex just didn't work. When I converted periods to [@XXX@] and removed all of those, it worked. BLEAUGH.

 
 
January 21, 2014, at 03:06 PM by OtherMichael - some sample code
Changed lines 41-42 from:
replace all whitespace of more than one character: [@\s-\{2,\}@]
to:
!!! replace all whitespace of more than one character: [@\s-\{2,\}@]
Added lines 47-52:

!!! Find all 4-digit numbers (interactive)
[@\([0-9]\{4\}\)@]

!!! Find all sequences of 3 or 4 uppercase letters
[@\([[:upper:]]\{3,4\}\)@]
 
 
December 19, 2013, at 09:42 AM by OtherMichael - replace whitespace
Added lines 35-45:
(:sourceend:)


[[#examples]]
!! practical applications

replace all whitespace of more than one character: [@\s-\{2,\}@]

(:source lang=lisp:)
(while (re-search-forward "\\s-\\{2,\\}" nil t)
      (replace-match ""))
 
 
January 06, 2009, at 08:50 AM by OtherMichael - move pronounciation to main page
Deleted lines 35-40:


!! Pronunciation
You know, "regexp" is just plain stupid, and unpronouncable. "Regex" is so much easier.

[[http://regexadvice.com/blogs/wayneking/archive/2004/02/01/272.aspx|Wayne hits the nail on the head]] - except for the whole "soft 'g'" thing.
 
 
January 03, 2009, at 10:25 AM by OtherMichael - semantic links, re-org, prononciation
Changed lines 2-11 from:
http://www.emacswiki.org/cgi-bin/wiki/CategoryRegexp

http://www
.emacs.uniyar.ac.ru/doc/em24h/emacs075.htm - 24 hours, Regexp chapter

http://steve.yegge.googlepages.com/effective-emacs#item9

http://web.ics.purdue.edu/~yoder2/hints/installs/emacs-regexp.txt

http://www.emacswiki.org/cgi-bin/wiki/EmacsCrashRegexp

to:
EmacsWiki:CategoryRegexp
EmacsWiki:EmacsCrashRegexp
[[http:
//www.emacs.uniyar.ac.ru/doc/em24h/emacs075.htm|Emacs in 24 hours, Regexp chapter]]
Steve Yegge's [[
http://steve.yegge.googlepages.com/effective-emacs#item9|Effective Emacs Regex section]]
[[
http://web.ics.purdue.edu/~yoder2/hints/installs/emacs-regexp.txt|(somebody's) guide to Emacs' Regexes]]

Changed lines 38-43 from:
to:
!! Pronunciation
You know, "regexp" is just plain stupid, and unpronouncable. "Regex" is so much easier.

[[http://regexadvice.com/blogs/wayneking/archive/2004/02/01/272.aspx|Wayne hits the nail on the head]] - except for the whole "soft 'g'" thing.

Changed line 48 from:
[[!Emacs]] [[!regexps]] [[!RegularExpressions]]
to:
[[!Emacs]] [[!regexps]] [[!RegularExpressions]] [[!regexes]]
 
 
May 07, 2008, at 10:48 AM by MichaelPaulukonis -
Added lines 9-10:

http://www.emacswiki.org/cgi-bin/wiki/EmacsCrashRegexp
 
 
February 06, 2008, at 09:33 AM by MichaelPaulukonis -
Added lines 24-38:

!! Differences between interactive and programmatic
The above escapes are different for programmatic use, versus interactive usage.

slash-marks need to be escaped, so everything gets WORSE

(:source lang=lisp:)
M-x replace-regexp RET \(fw.getfieldsdata(.*)\{1\}\) RET trim$(\1)
(:sourceend:)

(:source lang=lisp:)
  (replace-regexp "\\(fw.getfieldsdata(.*)\\{1\\}\\)" "trim$(\\1)")
(:sourceend:)