Regex for XML Namespace
Regex Pattern
xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"XML namespace declaration (xmlns or xmlns:prefix)
Quick Answer
The regex pattern for xml namespace is `xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"`. XML namespace declaration (xmlns or xmlns:prefix). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| xmlns="http://www.w3.org/2000/svg" | ✓ Matches |
| xmlns:xlink="http://www.w3.org/1999/xlink" | ✓ Matches |
| class="value" | ✗ No match |
| xmlns | ✗ No match |
| xmlns= | ✗ No match |
Code Examples
javascript
const regex = /xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"/; const isValid = regex.test(value);
python
import re
pattern = r'xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"'
if re.match(pattern, value):
print("valid")ruby
pattern = /xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"/ if value =~ pattern puts "valid" end
php
if (preg_match('/xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\s*=\s*"[^"]*"/', $value)) {
echo "valid";
}java
String pattern = "xmlns(?::[a-zA-Z][a-zA-Z0-9]*)?\\s*=\\s*"[^"]*""; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
HTML Tag
Any HTML tag (opening, closing, or self-closing)
HTML Comment
HTML comment block (multi-line safe)
HTML Attribute
HTML attribute key-value pair (attr="value")
HTML Self-Closing Tag
Self-closing HTML tags like <br/>, <img/>, <input/>
HTML Entity
HTML character entity (named or numeric)
Semantic HTML5 Tag
HTML5 semantic element tags