<blockquote>
<p>Does anyone know if there is anything special about the snakemake regexes? Should they not be normal python regexes?</p>
</blockquote>
<p>No, nothing special.  The <a href="https://snakemake.readthedocs.io/en/stable/snakefiles/rules.html#wildcards" rel="nofollow noreferrer">Snakemake documentation</a> just links to the <a href="https://docs.python.org/3/library/re.html" rel="nofollow noreferrer">Python re module documentation</a>.</p>
<p>I think this is something strange in how the anchors <code>^</code> and <code>$</code> behave when matching wildcards, and not anything else in your particular regular expression.  Check out the pattern-matching with this dummy Snakefile:</p>
<pre><code>rule all:
    input: [&quot;data/subdir/example&quot;, &quot;data/example&quot;, &quot;example&quot;]

rule anchored_example_of_subdir:
    &quot;&quot;&quot;Can't add either ^ or $ or this will fail&quot;&quot;&quot;
    output: &quot;data/{subdir,subdir}/example&quot;

rule anchored_example_in_subdir:
    &quot;&quot;&quot;Can't add ^ or this wil fail&quot;&quot;&quot;
    output: &quot;data/{file,example$}&quot;

rule anchored_example:
    &quot;&quot;&quot;This works with both ^ and <span class="math-container">$"""
output: "{file,^example$</span>}&quot;
</code></pre>
<p>(There are no inputs or actions so it's just a matter of what filenames match what rules, and the regular expressions do nothing but match exact strings.)  This &quot;works&quot; as is (<code>snakemake -nrp</code> shows what you expect) but if you add in anchors as described in the docstrings above the upper two rules no longer match the expected filenames.  It's as if (and maybe is?) under the hood Snakemake is trying to apply the expression to the entire path.</p>
<p>Can you just drop your <code>^</code> and <code>$</code>?  The snakemake documentation implies that it's using the equivalent of <code>re.match</code> rather than <code>re.search</code> already, with wording like &quot;Here we could restrict the wildcard <code>dataset</code> to consist of digits only using <code>\d+</code> as the corresponding regular expression&quot; so I don't think it's necessary anyway.</p>
<hr />
<p>Edit: I think I found the issue <a href="https://github.com/snakemake/snakemake/blob/b5a89cc8dd359c2eb726acf55385a1ab89fa871c/snakemake/io.py#L859" rel="nofollow noreferrer">in the snakemake source</a>.  It munges your constraints together into one larger regular expression for the entire file path, so <code>^</code> and <code>$</code> get embedded <em>inside</em> the resulting expression.  This looks like something that should at least be clarified in the documentation.</p>

CC BY-SA 4.0
2020-12-04T06:23:10.277Z
Thank you for your answer! I will report this as an issue then.
Sebastian Schmidt
CC BY-SA 4.0
2020-12-03T16:02:25.517Z
2020-12-03T16:27:35.300Z
2020-12-03T16:27:35.300Z
Jesse
<p>I have the following two rules:</p>
<pre><code>rule extract:
    input: &quot;data/{dir}/{file}.gz&quot;
    output: &quot;data/{dir}/{file,^.*(?&lt;!\.gz)$}&quot;
    conda: &quot;config/conda-extract-env.yml&quot;
    shell: &quot;cd 'data/{wildcards.dir}'; gunzip -k {wildcards.file}.gz&quot;

rule extract_dot:
    input: &quot;data/{dir}/wtdbg2.3.dot.gz&quot;
    output: &quot;data/{dir}/wtdbg2.3.dot&quot;
    conda: &quot;config/conda-extract-env.yml&quot;
    shell: &quot;cd 'data/{wildcards.dir}'; gunzip -k wtdbg2.3.dot.gz&quot;
</code></pre>
<p>The second one is actually a hack because the first does not work on files named <code>wtdbg2.3.dot</code>.
I have also tried giving the constraint using the <code>wildcard_constraints</code> member of the rule, and prefixed the string with an <code>r</code> or not.</p>
<p>Whatever I do, as soon as I uncomment the <code>extract_dot</code> rule, snakemake complains about the missing input file <code>data/C.elegans/wtdbg2.3.dot</code> in another rule. Note that there is a rule with output <code>data/C.elegans/wtdbg2.3.dot.gz</code>, and when changing this rule to output <code>data/C.elegans/wtdbg2.3.dot</code> instead, everything works as well.</p>
<p>So I conclude that the problem is in the regex. I have tested the regex on <a href="https://regex101.com/r/lwxU3e/1" rel="nofollow noreferrer">regex101.com</a>, and it accepts <code>wtdbg2.3.dot</code>. But snakemake seems not to. Does anyone know if there is anything special about the snakemake regexes? Should they not be normal python regexes?</p>

<snakemake>
Snakemake wildcard constraints reject too much
# Snakemake wildcard constraints reject too much

(Asked by: username_0 on 2020-12-03)

I have the following two rules:
rule extract:
    input: "data/{dir}/{file}.gz"
    output: "data/{dir}/{file,^.*(?
The second one is actually a hack because the first does not work on files named wtdbg2.3.dot.
I have also tried giving the constraint using the wildcard_constraints member of the rule, and prefixed the string with an r or not.
Whatever I do, as soon as I uncomment the extract_dot rule, snakemake complains about the missing input file data/C.elegans/wtdbg2.3.dot in another rule. Note that there is a rule with output data/C.elegans/wtdbg2.3.dot.gz, and when changing this rule to output data/C.elegans/wtdbg2.3.dot instead, everything works as well.
So I conclude that the problem is in the regex. I have tested the regex on regex101.com, and it accepts wtdbg2.3.dot. But snakemake seems not to. Does anyone know if there is anything special about the snakemake regexes? Should they not be normal python regexes?

**Tags:** snakemake

---

## 1 answers

### Answer by username_1 on 2020-12-03. Score: 2 | Accepted answer

Does anyone know if there is anything special about the snakemake regexes? Should they not be normal python regexes?

No, nothing special.  The Snakemake documentation just links to the Python re module documentation.
I think this is something strange in how the anchors ^ and $ behave when matching wildcards, and not anything else in your particular regular expression.  Check out the pattern-matching with this dummy Snakefile:
rule all:
    input: ["data/subdir/example", "data/example", "example"]

rule anchored_example_of_subdir:
    """Can't add either ^ or $ or this will fail"""
    output: "data/{subdir,subdir}/example"

rule anchored_example_in_subdir:
    """Can't add ^ or this wil fail"""
    output: "data/{file,example$}"

rule anchored_example:
    """This works with both ^ and $"""
output: "{file,^example$}"

(There are no inputs or actions so it's just a matter of what filenames match what rules, and the regular expressions do nothing but match exact strings.)  This "works" as is (snakemake -nrp shows what you expect) but if you add in anchors as described in the docstrings above the upper two rules no longer match the expected filenames.  It's as if (and maybe is?) under the hood Snakemake is trying to apply the expression to the entire path.
Can you just drop your ^ and $?  The snakemake documentation implies that it's using the equivalent of re.match rather than re.search already, with wording like "Here we could restrict the wildcard dataset to consist of digits only using \d+ as the corresponding regular expression" so I don't think it's necessary anyway.

Edit: I think I found the issue in the snakemake source.  It munges your constraints together into one larger regular expression for the entire file path, so ^ and $ get embedded inside the resulting expression.  This looks like something that should at least be clarified in the documentation.

> **Comments:**
> **username_0**: Thank you for your answer! I will report this as an issue then.  

---

