简体   繁体   中英

Finding the second last occurrence of a string (date) in Regex

I got the following strings:

(1640.31; 08/19/2016; 09/13/2016;); (250000.0; 09/30/2016; 02/17/2018;); (100000.0; 03/12/2018; 12/31/2025;);

Or

(1000000.0; 05/30/2018; 06/03/2028;);   

I need to return this second to last date, so in these cases for example 1: 03/12/2018 and example 2: 05/30/2018.

Because there are a lot of string-parts ending with ; I can't figure quite out how I can get the second to last date.

I think this does what you want:

select (select array_agg(val order by o desc limit 2)  -- the limit is just for efficiency
        from unnest(split(str, ';')) val with offset o
        where val like '%/%/%'
       )[ordinal(2)] a
from (select '1640.31; 08/19/2016; 09/13/2016;' as str) x;

Note that this also (happens to) work with parentheses, if they are really part of the strings.

Below example for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '(1640.31; 08/19/2016; 09/13/2016;); (250000.0; 09/30/2016; 02/17/2018;); (100000.0; 03/12/2018; 12/31/2025;);' AS str UNION ALL
  SELECT '(1000000.0; 05/30/2018; 06/03/2028;);'
)
SELECT ARRAY_REVERSE(REGEXP_EXTRACT_ALL(str, r'\d\d/\d\d/\d\d\d\d'))[SAFE_OFFSET(1)] dt
FROM `project.dataset.table`   

with result:

Row dt   
1   03/12/2018   
2   05/30/2018   

note: above assumes that dates are always in mm/dd/yyyy or dd/mm/yyyy format, but can be adjusted if different

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM