A text comes into the variable that can contain 2 or more consecutive @ characters.

Example 1:

It is necessary to delete it if there are two or more @ symbols in the text in a row.

For example, the text contains:

  1. 2 @ 2 - do not touch.
  2. and if like this @@ - remove both characters.

It is necessary to delete all the characters in the string that contains two or more dogs in a row, or one dog after which there is no symbol.

But the text may contain two examples at once and it is important that the text from the first example remain unharmed.

Example 2:

Such an option is possible: if there are characters in the line before and after the dog that are not spaces, delete everything else.

Options that need to be removed:

  1. r @;
  2. @;
  3. @r;
  4. @@@@@@@;

Who already did tell me how to solve?

Closed due to the fact that the essence of the issue is incomprehensible to the participants by Grundy , aleksandr barakin , Dementiy1999 , VenZell , Saidolim 25 Apr '16 at 19:06 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    and if there are 3 dogs? Those. "@@@" - what should happen? One dog ("@") or zero dogs ("")? If dogs are zero, then write a regular season like this: /@@+/ (i.e., preg_replace('/@@+/', '', '@ @@ @@@ @a@') ) - BOPOH
  • one
    And if @d@ also replaced? or only when characters go in a row? - Grundy
  • @BOPOH, just if the dog after it is a symbol, then do nothing, and if the dog after which the dog or the space, then delete the dog - Dementiy1999
  • @Grundy, if such a case, then yes too - Dementiy1999
  • one
    well then you can: (preg_replace('/@@+|@(?=(\s|\r|\n|$))/', '', '@a @ @@ @@@@@ @a@') (at the output we get "@a @a" ). And if you delete if there is nothing in front of the dog, then this is: preg_replace('/@@+|@(?=\s|$|\r|\n)|(?<=\s|^|\r|\n)@/', '', '@aa@a @a @ @@ @@@@@ @a@') (output "aa @ aa a " ) - BOPOH

1 answer 1

Regular expressions have the construct {x, y}, where x is the minimum number of characters and y is the maximum. that is, in your case, preg_replace('/@{2,}/', '', $string_with_at)) will replace the matches, where 2 or more @, and preg_replace('/@{2}/', '', $string_with_at)) - will replace only double entry.

  • one
    see comments, such a regular season does not fit - Grundy
  • Thank you all very much for your participation - Dementiy1999