1

I have the following string

Re: Re: Re: Re: Re: Re: Re: Re:

What I want is the end result of mutating this string is:

Re:

So what I am looking for is a way to detect multiple Re: (with a space in between) and change it to just one Re:.

If I had any idea how to accomplish this I'd post some code but no real clue how to do this in php

1 Answer 1

8

You can use this regex:

<?php
$s = "Re: Re: Re: Re: Re: Re: Re: Re: Prueba";
$s = preg_replace("/(Re: ?){2,}/i", "Re: ", $s);
var_dump($s); // Re: Prueba

Explanation:

(Re: ?) is just Re: and an optional space between them.
{2,}    means 2 or more times (if it's just one, why replace it)
i       so it's case insensitive

Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.