Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Match multiple substrings and concatenate

Given I have

KsKd2s4dAs =AhAd2s4dKs =AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c =AdQdKdJdTd =AhQhKhJhTh =AsQsKsJsTs

I want to match all the substrings starting with '=' and concatenate them with this sign.

Output should be

KsKd2s4dAs AhAd2s4dKs=AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c AdQdKdJdTd=AhQhKhJhTh=AsQsKsJsTs

I am able to capture by using this regexp (=.{10}\s?)+ but failing to find a convenient way to make such string.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can use

preg_replace('~(?:\G(?!\A)\s+(?==)|(?<!\S)=)(\S+)~', '$1', $text)

See the regex demo. Details:

  • (?:\G(?!\A)\s+(?==)|(?<!\S)=) - either a = char at the start of string or right after a whitespace or the end of the previous match and then one or more whitespaces followed with a = char
  • (\S+) - Group 1: one or more non-whitespace chars.

Another way is using preg_replace_callback:

$text = 'KsKd2s4dAs =AhAd2s4dKs =AsAd2s4dKs 5s5d6s6d2c AsAd6s6d2c =AdQdKdJdTd =AhQhKhJhTh =AsQsKsJsTs';
echo preg_replace_callback('~=(\S+(?:\s+=\S+)*)~', function($m) {
    return preg_replace('~\s+~', '', $m[1]);
}, $text);

See the PHP demo and the regex demo.

The =(\S+(?:\s+=\S+)*) matches a = and then captures into Group 1 any one or more non-whitespaces followed with zero or more repetitions of one or more whitespaces, =, and one or more non-whitespaces.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda