Regular Expression for deleting all datetimes from conversation

时间:2016-04-04 17:44:05

标签: regex swift

I have string with new lines in swift like this:

How can I remove all timestamps(11.03.2016 22:46:16) from that string ?

3 个答案:

答案 0 :(得分:0)

Simplest one for your situation could be search with following regex and replace with empty string.

Regex: ^[\d.: ]+: and replace with empty string.

Regex101 Demo

答案 1 :(得分:0)

The simplest approach would be a character class combined with anchors:

^[\d. :]+(.+)
# anchor it to the beginning of the line
# only digits, spaces, dots and a colon allowed
# one or more times
# the (.+) brings you down the line

Take only the first captured group here.
See a demo for this approach here on regex101.com

答案 2 :(得分:0)

The most accurate solution with the fewest false matches would be this:

^\d{2}.\d{2}.\d{4} \d{2}.\d{2}.\d{2}:

You'll want to use the regex multiline and global flags too.