<p>For DER sigs, it's possible for R or S to be very small.</p>

<ol>
<li>DER sigs: <code>re.compile('^30[0-9a-fA-F]{72,136}02[0-9a-fA-F]{0,64}02[0-9a-fA-F]{0,64}[0-9a-fA-F]{8}')</code></li>
</ol>

<p>Bitcoin addresses cannot have a 0. Also the {1} is superfluous</p>

<ol start="2">
<li>Bitcoin addresses: <code>re.compile('^[123mn][a-km-zA-HJ-NP-Z1-9]{26,33}$')</code></li>
</ol>

<p>Again, {1} is superfluous for OP_RETURN. Also, the a might be capitalized.</p>

<ol start="3">
<li>OP Return hex strings: <code>re.compile('^6[aA][a-fA-F0-9]{0,80}$')</code></li>
</ol>

<p>TXIDs are fine</p>

CC BY-SA 3.0
2015-11-27T15:12:29.790Z
Ring a ding ding. Thank you **for answering the question**: I get that it's SO and pedantic semantics are to be expected...  +250 bounty headed your way for the concise answer!
Wizard Of Ozzie
CC BY-SA 3.0
2015-11-27T22:22:21.670Z
@WizardOfOzzie You should probably test the regex before accepting the answer.
Claris
CC BY-SA 3.0
2015-11-28T01:18:25.190Z
You might want to consider using '([0-9a-fA-F][0-9a-fA-F]){X}' instead of '[0-9a-fA-F]{2X}', to capture the fact that the hex chars must come in pairs.
morsecoder
CC BY-SA 3.0
2015-12-07T08:59:11.100Z
I ended up using: `ur'''30(?P<siglen>[0-4][0-9a-f])02(?P<rlen>[0-2][0-9a-f])(?P<r>(?:00)?[a-f0-9]{2,64})02(?P<slen>[0-2][0-9a-f])(?P<s>(?:00)?[a-f0-9]{2,64})(?P<sighash>(0|8)[0-3])?'''`
Wizard Of Ozzie
CC BY-SA 3.0
2015-11-27T15:07:30.793Z
2015-11-27T15:07:30.793Z
Jimmy Song
<p>I'm using Python (2.7) and its <code>re</code> module for recognition of various Bitcoin data, namely:  </p>

<blockquote>
  <p>addresses, DER sigs, OP Return hexdata, TxIDs</p>
</blockquote>

<p>I've been using <code>re.compile</code>: for example, for a valid Tx hash (TxID):</p>

<pre><code>RE_TXHASH = re.compile('^[0-9a-fA-F]{64}$')
if bool(re.match(RE_TXHASH, 'f'*64)):    # 'f'*63 would fail
    print 'valid tx hash!'
else:
    raise Exception("invalid tx hash!")
</code></pre>

<p>I'm looking for regex patterns for the following (or input on my current best attempt):</p>

<ol>
<li><p>DER signatures: General format <em><code>"30[sig_size]02[r_size]02[s_size][sighash]"</code></em></p></li>
<li><p>Bitcoin addresses:  <code>re.compile('^[123mn]{1}[a-km-zA-HJ-NP-Z0-9]{26,33}$')</code></p></li>
<li><p>OP Return hex strings: <code>re.compile('^(6a){1}[a-fA-F0-9]{0,80}$')</code></p></li>
<li><p>TxIDs: <code>re.compile('^[0-9a-fA-F]{64}$')</code></p></li>
</ol>

<p><strong>EDIT</strong>: to clarify, can someone help with a regex pattern for this? <em><code>"30[sig_size]02[r_size]02[s_size][sighash]"</code></em></p>

CC BY-SA 3.0
2015-11-26T10:13:31.877Z
Not all signatures in the Bitcoin block chain are DER. `OP_RETURN` prefixed outputs don't have to be 80 bytes.
Claris
CC BY-SA 3.0
2015-11-26T13:51:26.920Z
[quote]Not all signatures in the Bitcoin block chain are DER.[/quote] Is there an example for non-DER signature in blockchain?
amaclin
CC BY-SA 3.0
2015-11-26T15:13:55.027Z
@Bitcoin I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern. Re OP_RETURN, isn't the regex saying between 0 and 80? (Honest question)
Wizard Of Ozzie
CC BY-SA 3.0
2015-11-26T15:28:19.460Z
@amaclin All of the ones with unnecessary amounts of padding on the values are BER not DER. You must use the smallest possible encoding for DER rules.
Claris
CC BY-SA 3.0
2015-11-27T01:19:19.567Z
Yes the regex is saying between one and eighty, but that's not a requirement of the data type.
Claris
CC BY-SA 3.0
2015-11-27T01:50:41.523Z
WRT the OP_RETURN: You're missing the data push byte. The data doesn't start immediately after the return. Also, 80 bytes is standard, but not required by consensus.
Nick ODell
CC BY-SA 3.0
2015-11-27T02:10:13.307Z
@NickODell Expecting a certain data push type is ill advised, push types are malleable and not always used consistently. 80 bytes you could use `0x50` for a direct push, `0x4c50` for `PUSHDATA1`, or `0x4d0050` for `PUSHDATA2`, or `0x4e00000050` for `PUSHDATA4`. Regular expressions isn't ideal for this task, especially for BER signatures.
Claris
CC BY-SA 3.0
2015-11-27T02:12:41.793Z
@amaclin Some altcoins also now contain the bytes to trigger the OpenSSL 32bit consensus failure.
Claris
CC BY-SA 3.0
2015-11-27T15:15:47.037Z
@Bitcoin How did we reach discussing altcoins, DER/BER and OP_RETURN ahead of my question on regexs? **Me:** `I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern.` seems pretty clear and independent of DER/BER classification
Wizard Of Ozzie
<python>
Regular Expressions for DER signature hex (also req, address, TxIDs)?
Wizard Of Ozzie
# Regular Expressions for DER signature hex (also req, address, TxIDs)?

(Asked by: username_0 on 2015-11-26)

I'm using Python (2.7) and its `re` module for recognition of various Bitcoin data, namely:

> addresses, DER sigs, OP Return hexdata, TxIDs

I've been using `re.compile`: for example, for a valid Tx hash (TxID):

```
RE_TXHASH = re.compile('^[0-9a-fA-F]{64}$')
if bool(re.match(RE_TXHASH, 'f'*64)):    # 'f'*63 would fail
    print 'valid tx hash!'
else:
    raise Exception("invalid tx hash!")

```

I'm looking for regex patterns for the following (or input on my current best attempt):

1. DER signatures: General format *`"30[sig_size]02[r_size]02[s_size][sighash]"`*
2. Bitcoin addresses: `re.compile('^[123mn]{1}[a-km-zA-HJ-NP-Z0-9]{26,33}$')`
3. OP Return hex strings: `re.compile('^(6a){1}[a-fA-F0-9]{0,80}$')`
4. TxIDs: `re.compile('^[0-9a-fA-F]{64}$')`

**EDIT**: to clarify, can someone help with a regex pattern for this? *`"30[sig_size]02[r_size]02[s_size][sighash]"`*

**Tags:** python

> **Comments:**
> **username_1**: Not all signatures in the Bitcoin block chain are DER. `OP_RETURN` prefixed outputs don't have to be 80 bytes.  
> **username_2**: [quote]Not all signatures in the Bitcoin block chain are DER.[/quote] Is there an example for non-DER signature in blockchain?  
> **username_0**: @Bitcoin I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern. Re OP_RETURN, isn't the regex saying between 0 and 80? (Honest question)  
> **username_1**: @username_2 All of the ones with unnecessary amounts of padding on the values are BER not DER. You must use the smallest possible encoding for DER rules.  
> **username_1**: Yes the regex is saying between one and eighty, but that's not a requirement of the data type.  
> **username_3**: WRT the OP_RETURN: You're missing the data push byte. The data doesn't start immediately after the return. Also, 80 bytes is standard, but not required by consensus.  
> **username_1**: @username_3 Expecting a certain data push type is ill advised, push types are malleable and not always used consistently. 80 bytes you could use `0x50` for a direct push, `0x4c50` for `PUSHDATA1`, or `0x4d0050` for `PUSHDATA2`, or `0x4e00000050` for `PUSHDATA4`. Regular expressions isn't ideal for this task, especially for BER signatures.  
> **username_1**: @username_2 Some altcoins also now contain the bytes to trigger the OpenSSL 32bit consensus failure.  
> **username_0**: @Bitcoin How did we reach discussing altcoins, DER/BER and OP_RETURN ahead of my question on regexs? **Me:** `I'm looking to recognise just DER signatures with regex, so I'm open to any suggestions for the regex pattern.` seems pretty clear and independent of DER/BER classification  

---

## 1 answers

### Answer by username_4 on 2015-11-27. Score: 2 | Accepted answer

For DER sigs, it's possible for R or S to be very small.

1. DER sigs: `re.compile('^30[0-9a-fA-F]{72,136}02[0-9a-fA-F]{0,64}02[0-9a-fA-F]{0,64}[0-9a-fA-F]{8}')`

Bitcoin addresses cannot have a 0. Also the {1} is superfluous

2. Bitcoin addresses: `re.compile('^[123mn][a-km-zA-HJ-NP-Z1-9]{26,33}$')`

Again, {1} is superfluous for OP\_RETURN. Also, the a might be capitalized.

3. OP Return hex strings: `re.compile('^6[aA][a-fA-F0-9]{0,80}$')`

TXIDs are fine

> **Comments:**
> **username_0**: Ring a ding ding. Thank you **for answering the question**: I get that it's SO and pedantic semantics are to be expected...  +250 bounty headed your way for the concise answer!  
> **username_1**: @username_0 You should probably test the regex before accepting the answer.  
> **username_5**: You might want to consider using '([0-9a-fA-F][0-9a-fA-F]){X}' instead of '[0-9a-fA-F]{2X}', to capture the fact that the hex chars must come in pairs.  
> **username_0**: I ended up using: `ur'''30(?P<siglen>[0-4][0-9a-f])02(?P<rlen>[0-2][0-9a-f])(?P<r>(?:00)?[a-f0-9]{2,64})02(?P<slen>[0-2][0-9a-f])(?P<s>(?:00)?[a-f0-9]{2,64})(?P<sighash>(0|8)[0-3])?'''`  

---

