[英]Regex for last occurence of String between fix determinator
所以我在以下结构中返回了一个特定的字符串:
"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"
我只想从格式“YYYY-MM-dd”中找到最后一次出现的日期,因为它也可能是值“false”可能正在返回一个日期。
我在正则表达式方面没有经验,但我实现的最后一件事是使用以下表达式接收2017-12-12"*
:
**((?:[^"]"*){10})$**
以所需格式查找日期的出现无济于事,因为可能会出现多个日期。
这就是为什么我要检查引号之间的最后一个字符串。 我如何得到这个没有引号的日期?
我想检查引号之间的最后一个字符串
只是:
regexp_replace(myvalue, '^.*"([^"]+)"$', '\1')
正则表达式分解:
^ beginning of the string
.* any sequence of 0 to N characters
" double quote
( beginning of the capturing group
[^"]+ as many characters as possible other than a double quote (at least one)
) end of the capturing group
" double quote
$ end of string
正则表达式匹配整个字符串并将其替换为捕获的部分。
with t as (select '"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"' myvalue from dual)
select regexp_replace(myvalue, '^.*"([^"]+)"$', '\1') mydate from t
| MYDATE | | :--------- | | 2017-12-13 |
如果需要,您可以通过在捕获组中指定预期的日期格式来更具体:
regexp_replace(myvalue, '^.*"(\d{4}-\d{2}-\d{2})"$', '\1')
您可以使用以下正则表达式来执行此操作:
/^(?:".*?",)*"(.*?)"$/
或者,您可以使用String#split()
和String#slice()
:
const output=input
.split(',')
.pop()
.slice(1,-1)
如果我理解正确, "false", "false", "2017-12-13"
可以是三个 date 或三个false
的任意组合,或者介于两者之间的任何组合,并且您正在寻找 last date 。 我会用
(\d{4}-\d\d-\d\d(?!.*\d{4}-\d\d-\d\d))
只要它后面没有另一个日期,它将捕获一个日期。
在此处查看我的示例:
https://regex101.com/r/GAkpDI/1/
"http://www.google.com/search","XYZ","Some other Value","2018-04-09","false","<<2017-12-13>>"
"http://www.google.com/search","XYZ","Some other Value","<<2018-12-09>>","false","false"
"http://www.google.com/search","XYZ","Some other Value","false","<<2000-09-17>>","false"
"http://www.google.com/search","XYZ","Some other Value","2018-12-09","2000-09-17","<<2017-12-13>>"
我把火柴放在<<>>
里面。
编辑:如果您不知道日期的格式,那么您可以将它换成只找到数字、破折号和斜杠的东西:
https://regex101.com/r/GAkpDI/2/
([\d-\/]+(?!.*[\d-\/]+))
这将更有可能失败,因为它会找到任何东西[0123456789-/]
,但是如果您的程序只会在其中放置一个日期或false
,它应该仍然是可行的。
使用简单的regexp_substr
如下:
select regexp_substr('"http://www.google.com/search","XYZ","Some other Value","false","false","2017-12-13"',
'((\d){4}(-)(\d){2}(-)(\d){2})"$',1,1,null,1)
from dual;
这里, regexp_substr
的参数如下:
REGEXP_SUBSTR( string, pattern [, start_position [, nth_appearance [, match_parameter [, sub_expression ] ] ] ] )
干杯!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.