1

For example, I have a string like this:

Line 1
<br/>
<br/>
Line 2
<br/>
<br/>
<br/>
Line 3

I need to detect and replace the duplicated and attached string like the <br/>'s so it will be only 1 <br/> instead. So the final result I expect is:

Line 1
<br/>
Line 2
<br/>
Line 3

Anyone can help me with the regex? explanation will be appreciated.

2
  • What language? When you ask about regex on SO you should nearly always specify the language you are using. Commented Sep 3, 2011 at 19:21
  • @xanatos: In PHP, question edited. thanks. Commented Sep 3, 2011 at 19:24

2 Answers 2

2
preg_replace('/(.{3,})\\1+/s', '\\1', $string);

This will replace any sequence of duplicate strings of 3 chars or more.

Try it here: http://ideone.com/5ew7X

If you want to replace only the <br/>s:

preg_replace('/(<br\/>\s*){2,}/s', '\\1', $string);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, is that possible to specify a certain string to be cleaned, for example only "<br/>" will be cleaned.
1

Not the straight answer to your regex question, still consider doing it using sed; courtesy of Eric Pement's sed one-liners.

 # delete duplicate, consecutive lines from a file (emulates "uniq").
 # First line in a set of duplicate lines is kept, rest are deleted.
 sed '$!N; /^\(.*\)\n\1$/!P; D'

 # delete duplicate, nonconsecutive lines from a file. Beware not to
 # overflow the buffer size of the hold space, or else use GNU sed.
 sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'

 # delete all lines except duplicate lines (emulates "uniq -d").
 sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'

1 Comment

Sorry I don't give the information for the language I am going to use, is this possible to use with PHP?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.