2015-11-23 105 views
1

我想從使用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()函數看起來像它可能會做的伎倆,但我不能使用它。

回答

1

您應該將整個字符串匹配,組日期和跟團取代:

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

我不能對代碼進行測試,但希望你會得到的一般過程。

3

的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> 

因此,讓您匹配的字符串,你可以簡單地請求analyze-string($string, $regex)//fn:match