Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.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 从字符串中提取与正则表达式匹配的字符串_Regex_Xquery - Fatal编程技术网

Regex 从字符串中提取与正则表达式匹配的字符串

Regex 从字符串中提取与正则表达式匹配的字符串,regex,xquery,Regex,Xquery,我正在尝试使用XQuery从字符串中提取日期 let $string := "This string has a yymmdd date like 151123 in it." (: I need the date bit -> 151123 :) let $regex := "[0-9]{2}[0-1][0-9][0-3][0-9]" 使用replace()和tokenize()我可以得到匹配字符串以外的所有内容。functx:get-matches()函数看起来可能会起作用,但我无法

我正在尝试使用XQuery从字符串中提取日期

let $string := "This string has a yymmdd date like 151123 in it."
(: I need the date bit -> 151123 :)
let $regex := "[0-9]{2}[0-1][0-9][0-3][0-9]"

使用
replace()
tokenize()
我可以得到匹配字符串以外的所有内容。
functx:get-matches()
函数看起来可能会起作用,但我无法使用它。

您应该匹配整个字符串,将日期分组并替换为组:

fn:replace($string, "^.*([0-9]{2}[0-1][0-9][0-3][0-9]).*$", "$1")

我无法测试代码,但希望您能够获得一般流程。

您应该匹配整个字符串,将日期分组,并替换为组:

fn:replace($string, "^.*([0-9]{2}[0-1][0-9][0-3][0-9]).*$", "$1")

我无法测试代码,但希望您能了解一般流程。

XQuery 3.0具有出色的
analyze-string()
函数:

xquery version "3.0";

let $string := "This string has a yymmdd date like 151123 in it."
(: I need the date bit -> 151123 :)
let $regex := "[0-9]{2}[0-1][0-9][0-3][0-9]"
return 
    analyze-string($string, $regex)
返回:

<fn:analyze-string-result xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <fn:non-match>This string has a yymmdd date like </fn:non-match>
    <fn:match>151123</fn:match>
    <fn:non-match> in it.</fn:non-match>
</fn:analyze-string-result>

此字符串的yymmdd日期如下
151123
在里面。

因此,要获得匹配字符串,只需请求
analyze-string($string,$regex)//fn:match

analyze-string()功能:

xquery version "3.0";

let $string := "This string has a yymmdd date like 151123 in it."
(: I need the date bit -> 151123 :)
let $regex := "[0-9]{2}[0-1][0-9][0-3][0-9]"
return 
    analyze-string($string, $regex)
返回:

<fn:analyze-string-result xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <fn:non-match>This string has a yymmdd date like </fn:non-match>
    <fn:match>151123</fn:match>
    <fn:non-match> in it.</fn:non-match>
</fn:analyze-string-result>

此字符串的yymmdd日期如下
151123
在里面。
因此,要获得匹配字符串,只需请求
分析字符串($string,$regex)//fn:match

分析字符串($string,$regex)//fn:match/text()分析字符串($string,$regex)//fn:match/text()