diff --git a/docs/simplesamlphp-reference-idp-hosted.txt b/docs/simplesamlphp-reference-idp-hosted.txt
index 6ee0640596c64ae6b1c66029857769962ef5001b..3fa8b5f406ed7c291840ca803e05f6b88f9bf9f1 100644
--- a/docs/simplesamlphp-reference-idp-hosted.txt
+++ b/docs/simplesamlphp-reference-idp-hosted.txt
@@ -108,6 +108,8 @@ Common options
 `scope`
 :   An array with scopes for this IdP.
     The scopes will be added to the generated XML metadata.
+    A scope can either be a domain name or a regular expression
+    matching a number of domains.
 
 `userid.attribute`
 :   The attribute name of an attribute which uniquely identifies
diff --git a/lib/SimpleSAML/Metadata/SAMLBuilder.php b/lib/SimpleSAML/Metadata/SAMLBuilder.php
index 9238648079a552f035a23e19aa376e0b9475b261..da63e8991ae76a3fcc2918ca30edf9076a161784 100644
--- a/lib/SimpleSAML/Metadata/SAMLBuilder.php
+++ b/lib/SimpleSAML/Metadata/SAMLBuilder.php
@@ -114,7 +114,12 @@ class SimpleSAML_Metadata_SAMLBuilder {
 			foreach ($metadata->getArray('scope') as $scopetext) {
 				$s = new SAML2_XML_shibmd_Scope();
 				$s->scope = $scopetext;
-				$s->regexp = FALSE;
+				// Check whether $ ^ ( ) * | \ are in a scope -> assume regex.
+				if (1 === preg_match('/[\$\^\)\(\*\|\\\\]/', $scopetext)) {
+					$s->regexp = TRUE;
+				} else {
+					$s->regexp = FALSE;
+				}
 				$e->Extensions[] = $s;
 			}
 		}