Reputation: 3092
I'm trying to remove duplicate lines with regex in Xcode with Swift. Being a beginner, I'm having some difficulties to learn how to doing it.
Currently for using regEx, I'm trying to use this code (please note that where I'm using two backslashes, one of them it's used just as escape character for Swift strings) adapted from http://www.regular-expressions.info/duplicatelines.html:
let originalText = "Have a nice day \n\n Have a nice day"
let myPattern = "^(.*)(\\r?\\n\\1)+$"
let replacement = "\\1"
But it seems that it don't work in my implementation.
Upvotes: 1
Views: 168
Reputation: 15000
I'd modify your expression to this:
(^[^\n]*$)(?:\n\1)+
Replace With: \1
This regular expression will do the following:
Note: you'll need to replace the \
with \\
and use the following flags: Multiline so that ^
and $
match at each line, and the global option so the expression continues after the first match.
Live Demo
https://regex101.com/r/uJ9rS8/4
Sample text
Have a nice day
Have a nice day
fdaf
These are not the droids you are looking for
These are not the droids you are looking for
These are not the droids you are looking for
Sample Matches
Have a nice day
fdaf
These are not the droids you are looking for
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
[^\n]* any character except: '\n' (newline) (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
\1 what was matched by capture \1
----------------------------------------------------------------------
)+ end of grouping
----------------------------------------------------------------------
Upvotes: 1