如何检查字符串是否为有效的XML元素名称? [英] How to check if string is a valid XML element name?
问题描述
我需要用PHP的正则表达式或函数来验证字符串是否是一个好的XML元素名称.
I need a regex or a function in PHP that will validate a string to be a good XML element name.
w3schools表格:
Form w3schools:
XML元素必须遵循以下命名 规则:
XML elements must follow these naming rules:
- 名称可以包含字母,数字和其他字符
- 名称不能以数字或标点符号开头
- 名称不能以字母xml(或XML或Xml等)开头
- 名称不能包含空格
- Names can contain letters, numbers, and other characters
- Names cannot start with a number or punctuation character
- Names cannot start with the letters xml (or XML, or Xml, etc)
- Names cannot contain spaces
我可以编写一个基本的正则表达式来检查规则1,2和4,但是它不能解决所有允许的标点符号,也不能解决第三条规则
I can write a basic regex that will check for rules 1,2 and 4, but it won't account for all punctuation allowed and won't account for 3rd rule
\w[\w0-9-]
友好更新
名称和令牌
NameStartChar ::=
":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] |
[#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] |
[#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
[#x10000-#xEFFFF]
NameChar ::=
NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
Name ::=
NameStartChar (NameChar)*
还指定了单独的非令牌化规则:
Also a separate non-tokenized rule is specified:
以字符串"xml"开头或任何与(('X'|'x')('M'|'m')('L'|'l'))匹配的字符串的名称为保留用于本规范的此版本或将来版本中的标准化.
Names beginning with the string "xml", or with any string which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for standardization in this or future versions of this specification.
推荐答案
如何
/\A(?!XML)[a-z][\w0-9-]*/i
用法:
if (preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $subject)) {
# valid name
} else {
# invalid name
}
说明:
\A Beginning of the string
(?!XML) Negative lookahead (assert that it is impossible to match "XML")
[a-z] Match a non-digit, non-punctuation character
[\w0-9-]* Match an arbitrary number of allowed characters
/i make the whole thing case-insensitive
这篇关于如何检查字符串是否为有效的XML元素名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!