aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/regex.txx10
-rw-r--r--tests/regex/testscript10
2 files changed, 14 insertions, 6 deletions
diff --git a/libbutl/regex.txx b/libbutl/regex.txx
index 536cabf..dd1a539 100644
--- a/libbutl/regex.txx
+++ b/libbutl/regex.txx
@@ -169,9 +169,10 @@ namespace butl
if (si > 0)
{
- // m[0] refers to the matched substring.
+ // m[0] refers to the matched substring. Note that we
+ // ignore unmatched sub-expression references.
//
- if (static_cast<size_t> (si) < m.size ())
+ if (static_cast<size_t> (si) < m.size () && m[si].matched)
append_str (m[si].first, m[si].second);
}
else
@@ -212,9 +213,10 @@ namespace butl
int si (digit (c));
if (si > 0)
{
- // m[0] refers to the matched substring.
+ // m[0] refers to the matched substring. Note that we
+ // ignore unmatched sub-expression references.
//
- if (static_cast<size_t> (si) < m.size ())
+ if (static_cast<size_t> (si) < m.size () && m[si].matched)
append_str (m[si].first, m[si].second);
}
else
diff --git a/tests/regex/testscript b/tests/regex/testscript
index 1af604c..8291f36 100644
--- a/tests/regex/testscript
+++ b/tests/regex/testscript
@@ -21,7 +21,10 @@
: capture
:
- $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '$1$10' >aj
+ {
+ $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '$1$10' >aj : matched
+ $* a '(a)|(b)' '$1$2$3' >a : unmatched
+ }
}
: perl-escape
@@ -33,7 +36,10 @@
: capture
:
- $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '\1\10' >aa0
+ {
+ $* abcdefghij '(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)' '\1\10' >aa0 : matched
+ $* a '(a)|(b)' '\1\2\3' >a : unmatched
+ }
: upper
: