<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>Brief intro to regular expressions</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/03/03/Brief-intro-to-regular-expressions</link>
      <pubDate>Sun, 03 Mar 2013 15:04:31 EST</pubDate>
      <category><![CDATA[regular expressions]]></category>
      <guid isPermaLink="false">ScpgqHxzj0vYKKNHZX_nV7pJVio=</guid>
      <description>Brief intro to regular expressions</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://matlab.cheme.cmu.edu/2012/05/07/1701/" &gt;Matlab post&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
This example shows how to use a regular expression to find strings matching the pattern :cmd:`datastring`. We want to find these strings, and then replace them with something that depends on what cmd is, and what datastring is.
&lt;/p&gt;

&lt;p&gt;
Let us define some commands that will take datasring as an argument, and return the modified text. The idea is to find all the cmds, and then run them. We use python's &lt;code&gt;eval&lt;/code&gt; command to get the function handle from a string, and the cmd functions all take a datastring argument (we define them that way). We will create commands to replace :cmd:`datastring` with html code for a light gray background, and :red:`some text` with html code making the text red. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;text = &lt;span style="color: #228b22;"&gt;r'''Here is some text. use the :cmd:`open` to get the text into&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;          a variable. It might also be possible to get a multiline&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;            :red:`line     &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;     2` directive.'''&lt;/span&gt;

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; text
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; &lt;span style="color: #228b22;"&gt;'---------------------------------'&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
... ... &amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; Here is some text. use the :cmd:`open` to get the text into
          a variable. It might also be possible to get a multiline
            :red:`line     
     2` directive.
---------------------------------
&lt;/pre&gt;

&lt;p&gt;
Now, we define our functions.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;def&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;cmd&lt;/span&gt;(datastring):
    &lt;span style="color: #228b22;"&gt;' replace :cmd:`datastring` with html code with light gray background'&lt;/span&gt;
    s = &lt;span style="color: #228b22;"&gt;'&amp;lt;FONT style="BACKGROUND-COLOR: LightGray"&amp;gt;%{0}&amp;lt;/FONT&amp;gt;'&lt;/span&gt;;
    html = s.format(datastring)
    &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt; html

&lt;span style="color: #8b0000;"&gt;def&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;red&lt;/span&gt;(datastring):
    &lt;span style="color: #228b22;"&gt;'replace :red:`datastring` with html code to make datastring in red font'&lt;/span&gt;
    html = &lt;span style="color: #228b22;"&gt;'&amp;lt;font color=red&amp;gt;{0}&amp;lt;/font&amp;gt;'&lt;/span&gt;.format(datastring)
    &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt; html
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Finally, we do the regular expression. Regular expressions are hard. There are whole books on them. The point of this post is to alert you to the possibilities. I will break this regexp down as follows. 1. we want everything between :*: as the directive. ([^:]*) matches everything not a :. :([^:]*): matches the stuff between two :. 2. then we want everything between `*`. ([^`]*) matches everything not a `. 3. The () makes a group that python stores so we can refer to them later.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; re
regex = &lt;span style="color: #228b22;"&gt;':([^:]*):`([^`]*)`'&lt;/span&gt;
matches = re.findall(regex, text)
&lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; directive, datastring &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; matches:
    directive = &lt;span style="color: #8b0000;"&gt;eval&lt;/span&gt;(directive) &lt;span style="color: #ff0000; font-weight: bold;"&gt;# get the function&lt;/span&gt;
    text = re.sub(regex, directive(datastring), text)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; &lt;span style="color: #228b22;"&gt;'Modified text:'&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; text
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;gt;&amp;gt;&amp;gt; &amp;gt;&amp;gt;&amp;gt; ... ... ... &amp;gt;&amp;gt;&amp;gt; Modified text:
Here is some text. use the &amp;lt;FONT style="BACKGROUND-COLOR: LightGray"&amp;gt;%open&amp;lt;/FONT&amp;gt; to get the text into
          a variable. It might also be possible to get a multiline
            &amp;lt;FONT style="BACKGROUND-COLOR: LightGray"&amp;gt;%open&amp;lt;/FONT&amp;gt; directive.
&lt;/pre&gt;
&lt;p&gt;Copyright (C) 2013 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2013/03/03/Brief-intro-to-regular-expressions.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
