Extracting ICCID from a string using regex
<p>I'm trying to return and print the ICCID of a SIM card in a device; the SIM cards are from various suppliers and therefore of differing lengths (either 19 or 20 digits). As a result, I'm looking for a regular expression that will extract the ICCID (in a way that's agnostic to non-word characters immediately surrounding it).</p>

<p>Given that an ICCID is specified as a 19-20 digit string starting with "89", I've simply gone for:</p>

<pre><code>(89\d{17,18})
</code></pre>

<p>This was the most successful pattern that I'd tested (along with some patterns rejected for reasons below).</p>

<p>In the string that I'm extracting it from, the ICCID is immediately followed by a carriage return and then a line feed, but some testing against terminating it with <code>\r</code>, <code>\n</code>, or even <code>\b</code> failed to work (the program that I'm using is an in-house one built on python, so I suspect that's what it's using for regex). Also, simply using <code>(\d{19,20})</code> ended up extracting the last 19 digits of a 20-digit ICCID (as the third and last valid match). Along the same lines, I ruled out <code>(\d{19,20})?</code> in principle, as I expect that to finish when it finds the first 19 digits.</p>

<p><strong>So my question is:</strong> Should I use the pattern I've chosen, or is there a better expression (not using non-word characters to frame the string) that will return the longest substring of a variable-length string of digits?</p>

<p>If the engine behind the scenes is really Python, and there can be any non-digits chars around the value you need to extract, use lookarounds to restrict the context around the values:</p>

<pre><code>(?&lt;!\d)89\d{17,18}(?!\d)
^^^^^^^         ^^^^^^
</code></pre>

<p>The <code>(?&lt;!\d)</code> loobehind will require the absense of a digit before the match and <code>(?!\d)</code> negative lookahead will require the absence of a digit after that value.</p>

<p>See <a href="https://regex101.com/r/xU6bN5/1" rel="nofollow">this regex demo</a></p>

python regex string iccid