问题
My objective is to extract a date string and following characters using Regex in Google Sheets (sheets function: regexextract) where the string is the last line of a cell and starts with a date format "yyyy-DD-MM" followed by ":".
So the RegExpression that I currently have looks like:
\d{4}-\d{2}-\d{2}:.+
This works fine but it returns the first match. Instead I want to start at the end of the cell and extract the last match when there are multiple date strings. This is because the contents are stored ascending by date inside the cell.
Sample cell:
2020-05-20: Status update blah blah
2020-05-27: PO Issued blah blah
Result requested: I want the end result to be a string starting with date and the characters that follow "2020-05-27: PO Issued blah blah" which is the last result. However I always get the first match which in the example above is: "2020-05-20: Status update blah blah"
Also I'm doing this in google sheets using regexextract() which shouldn't make a difference in the regex but just wanted to mention it.
Edit: I found out Sheets is using RE2 so I guess it did make a difference.
回答1:
You may use
=REGEXEXTRACT(A1, "(?m)^\d{4}-\d{2}-\d{2}:.*\z")
See the RE2 regex demo and the Google Sheets screenshot:
The (?m)^\d{4}-\d{2}-\d{2}:.*\z
regex matches
(?m)
- a MULTILINE modifier that makes^
match start of a line and$
match end of a line^
- start of a line\d{4}-\d{2}-\d{2}:.*
- 4 digits,-
, 2 digit,-
, 2 digits,:
and then rest of the line since.
does not match line break chars by default\z
- the very end of the string (it is not affected by the(?m)
modifier).
Note that (?s).*\n(\d{4}-\d{2}-\d{2}:.*)
I suggested in the top comment below the question will match the last lines starting with a date, see a regex demo.
来源:https://stackoverflow.com/questions/62092799/regex-get-last-match-of-a-date-format-from-string-inside-a-google-sheets-cell