<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Brian Marx on Medium]]></title>
        <description><![CDATA[Stories by Brian Marx on Medium]]></description>
        <link>https://medium.com/@b.marx981?source=rss-1b6cb4d7b04c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VMd4zoHTNnzFLbpN.</url>
            <title>Stories by Brian Marx on Medium</title>
            <link>https://medium.com/@b.marx981?source=rss-1b6cb4d7b04c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 05:50:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@b.marx981/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Let’s get weird with Dart’s RexExp]]></title>
            <link>https://medium.com/@b.marx981/lets-get-weird-with-dart-s-rexexp-795c934519df?source=rss-1b6cb4d7b04c------2</link>
            <guid isPermaLink="false">https://medium.com/p/795c934519df</guid>
            <category><![CDATA[regular-expressions]]></category>
            <category><![CDATA[dart]]></category>
            <dc:creator><![CDATA[Brian Marx]]></dc:creator>
            <pubDate>Wed, 08 Jan 2025 01:10:32 GMT</pubDate>
            <atom:updated>2025-01-08T01:10:32.493Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>If you are reading this you are most likely interested in Dart’s Regular Expressions… right? If not, thanks for coming by I’m about to get weird with Dart’s RegExp.</p><p>Dart is a powerful language created by Google then open sourced years ago, but that’s old news. Dart has gained popularity recently due to its role as the programming language behind a the great cross platform framework Flutter (let’s be honest, that’s why you are really here).</p><p>In programming, we often encounter these curious things called Strings, and as programmers, we need to handle them in various ways. One really weird way is to interact with Strings is with regular expressions. So let the learning begin.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BesGukJPu-4r5iAD" /><figcaption>Photo by <a href="https://unsplash.com/@charlesdeluvio?utm_source=medium&amp;utm_medium=referral">charlesdeluvio</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The Dart way of interacting with Regular Expressions is through the RegExp class. It’s important to remember the RexExp objects take a <strong>raw string </strong>where you enter your regex. A raw string is used by entering an r before a string, I don’t know the exact reason why you need a raw string here but I believe it has something to do with the backslashes.</p><pre>// A simple regex that shows the r before a Dart String<br>// the RegExp constructor requires a raw string. <br>final reg = RegExp(r&#39;.&#39;); </pre><p>Another weird yet interesting fact is that, according to the Flutter docs for the RegExp class,</p><blockquote>Dart regular expressions have the same syntax and semantics as JavaScript regular expressions.</blockquote><p>So if you are dying for a regex answer that ChatGPT can’t answer StackOverflow JavaScript answers will work too.</p><figure><img alt="This is what came up when I looked for a thought bubble." src="https://cdn-images-1.medium.com/max/1024/0*TRh_Kmq-ryOhDnoV" /><figcaption>Photo by <a href="https://unsplash.com/@fakurian?utm_source=medium&amp;utm_medium=referral">Milad Fakurian</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As we start to get weird with Regular Expressions syntax we need to look at Characters Classes.</p><h3>Character Classes</h3><ul><li>Character Classes define sets of characters to match (or so says our fearless leader ChatGPT).</li></ul><p>If you want to find a single character in a string you need to use the dot followed by one or more characters.</p><pre>// To find one or more characters in a string use a <br>// dot then place your character(s).<br>String s = &quot;Hello all&quot;;<br>final reg = RegExp(r&#39;.a&#39;);<br>print(reg.hasMatch(s)); // true</pre><p>One thing to look out for is that if this character occurs at the beginning of the line it will return false.</p><pre>// The dot doesn&#39;t match if the character is at the beginning of the line<br>String s = &quot;a Hello&quot;;<br>final reg = RegExp(r&#39;.a&#39;);<br>print(reg.hasMatch(s)); // false</pre><p>Next up is the word match. \w matches all of the characters in a word.</p><pre>String word= &quot;Weird!* regex&quot;;<br>final reg = RegExp(r&#39;\w&#39;);<br>final m = reg.allMatches(word);<br>for(RegExpMatch element in m) {<br>  print(element.group(0)); //W,e,i,r,d,r,e,g,e,x<br>}</pre><p>And \W is the non-word character match. It represents any character that is not considered a “word character” which includes letters, numbers, and underscores.</p><pre>String word= &quot;Weird!*&quot;;<br>final reg = RegExp(r&#39;\W&#39;);<br>final m = reg.allMatches(word);<br>for(final element in m) {<br>  print(element[0]); //!,*<br>}</pre><p>Now we can dive into digits. We use \d if we want match digits and \D for anything that’s not a digit…. wait… what?</p><pre>String word= &quot;Weird!* 15 regex&quot;;<br>final reg = RegExp(r&#39;\d&#39;);<br>final m = reg.allMatches(word);<br>for(RegExpMatch element in m) {<br>  // I&#39;ll explain .group later. It&#39;s too much for now.<br>  print(element.group(0)); // 1,5<br>}</pre><pre>String word= &quot;Weird!* 15 regex&quot;;<br>final reg = RegExp(r&#39;\D&#39;);<br>final m = reg.allMatches(word);<br>for(RegExpMatch element in m) {<br>  print(element.group(0)); // W,e,i,r,d, , ,r,e,g,e,x &lt;- includes spaces.<br>}</pre><p>Finally we get to the space match \s. This will match any characters that are considered whitespace like spaces, tabs, newline etc.</p><pre>String word= &quot;Weird!* 15 regex&quot;;<br>final reg = RegExp(r&#39;\s&#39;);<br>final m = reg.allMatches(word);<br>for(RegExpMatch element in m) {<br>  print(&#39;${element.group(0)},&#39;); //[ , ,] &lt;- these are just blank strings.<br>}</pre><p>And the non space characters is:</p><pre>String word= &quot;Weird!* 15 regex&quot;;<br>final reg = RegExp(r&#39;\S&#39;);<br>final m = reg.allMatches(word);<br>for(RegExpMatch element in m) {<br>  print(element.group(0)); // W,e,i,r,d,!,*,1,5,r,e,g,e,x<br>}</pre><h3>Conclusion</h3><p>Wow what a weird approach to searching strings. So far all I’ve bored you with is the Character Class which are incredibly useful. Next we’ll hit the Quantifiers and Anchors. We have touched them so far. Let’s rest our heads for a little because I know mine hurts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=795c934519df" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>