Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用XPath正则表达式匹配URL_Regex_Xpath - Fatal编程技术网

Regex 如何使用XPath正则表达式匹配URL

Regex 如何使用XPath正则表达式匹配URL,regex,xpath,Regex,Xpath,需要XPath方面的帮助。我有这样一个XML: <unaryExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8"> <postfixExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8"> <leftHandSideExpression tokenV

需要XPath方面的帮助。我有这样一个XML:

   <unaryExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
      <postfixExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
        <leftHandSideExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
          <newExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
            <memberExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
              <primaryExpression tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
                <literal tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
                  <stringLiteral tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8">
                    <LITERAL tokenValue="'http://google.com'" tokenLine="1" tokenColumn="8"/>
                  </stringLiteral>
                </literal>
              </primaryExpression>
            </memberExpression>
          </newExpression>
        </leftHandSideExpression>
      </postfixExpression>
    </unaryExpression>
如何使用正则表达式查找url

(http://|https://|ftp://)([a-z0-9]{1})((\.[a-z0-9-])|([a-z0-9-]))*\.([a-z]{2,4})(\/?)

如果您的XPath引擎支持XPath 2.0,请对正则表达式使用
fn:matches
哪个等价物
fn:contains
。对于XPath1.0,不支持正则表达式

//LITERAL[fn:matches(@tokenValue, '(http://|https://|ftp://)([a-z0-9]{1})((\.[a-z0-9-])|([a-z0-9-]))*\.([a-z]{2,4})(/?)')]
将返回所有
-标记,这些标记具有与正则表达式匹配的
@tokenValue
-标记


表达式中存在一些问题,您不必(也可能不必)在最后一个匹配组中转义
/
。我在查询中修正了这个问题。为什么要使用最后两个匹配组?

正则表达式已准备就绪。不幸的是,修补程序2.0不受支持,因此必须查找链接,以便
//LITERAL[contains(@tokenValue,'http://a')]
//LITERAL[contains(@tokenValue,'http://b')]
//LITERAL[contains(@tokenValue,'http://c')]
等等。谢谢
//LITERAL[fn:matches(@tokenValue, '(http://|https://|ftp://)([a-z0-9]{1})((\.[a-z0-9-])|([a-z0-9-]))*\.([a-z]{2,4})(/?)')]