Regexps are weird in Emacs because of the lisp

Emacs in 24 hours, Regexp chapter
Steve Yegge’s Effective Emacs Regex section
(somebody’s) guide to Emacs’ Regexes



Why all the brackets and slashes, sometimes?

eg: "\\(\\[\\[#.*\\]\\]\\)\n!!!"
brackets [,] need escaping in a search regexp, but not in the replace regexp

The reason for needing to escape all of the control characters in an Emacs regexp is that the Emacs Lisp interpreter sees them prior to the regular expression engine. --Chris Smith


Newline in Emacs regexp

  1. in minibuffer: C-q C-j (see below)
  2. in code: \n


[...] insert a ^J character, which Emacs uses to represent newlines in functions and commands. At the point in the regexp or replacement where you need to insert a newline, hit Ctrl-q followed by Ctrl-j. Ctrl-q is Emacs’s “quote” command: rather than executing the following keystroke, Emacs will insert the key into the current buffer or the minibuffer. (Yegge)


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


M-x replace-regexp RET \(fw.getfieldsdata(.*)\{1\}\) RET trim$(\1)


(replace-regexp "\\(fw.getfieldsdata(.*)\\{1\\}\\)" "trim$(\\1)")



practical applications

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.



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


(while (re-search-forward "\\s-\\{2,\\}" nil t)
       (replace-match ""))


Find all 4-digit numbers (interactive)



Find all sequences of 3 or 4 uppercase letters



convert XML tags to UPPERCASE

I know there’s a better way of handling underscores.
But this here is today’s dirty-laundry....


This uses the replace-regexp evaluation-feature introduced in Emacs 22, which I read about waaaay back when in Steve Yegge’s 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


\,(upcase \1)


                        <mailing_name>Mr. Test Name</mailing_name>
                        <alpha_sort_name>NAME      TEST</alpha_sort_name>
                                <line_1_text>123 Main St.</line_1_text>
                                <city_name>Great Town</city_name>
                <error>No Error</error>


                        <MAILING_NAME>Mr. Test Name</MAILING_NAME>
                        <ALPHA_SORT_NAME>NAME      TEST</ALPHA_SORT_NAME>
                                <LINE_1_TEXT>123 Main St.</LINE_1_TEXT>
                                <CITY_NAME>Great Town</CITY_NAME>
                <ERROR>No Error</ERROR>


See Also

Programming.Regular Expression



Emacs regexps RegularExpressions regexes



Comment Page  

  seo plugin   - 11.10.2016 - 16:40 sites)

Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google Page Rank? or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword....wait there’s even more Now what if i told you there was a simple Wordpress plugin that does all the On-Page SEO, and automatically for you? That’s right AUTOMATICALLY, just watch this 4minute video for more information at. <a href=“ sites)”>Seo Plugin</a>



Add Comment

 Your Message
 Enter value ← Have you entered the code number?