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

328
Vistas
Sanitize phone number: regular expression match all except first occurence is on first position

regarding to this post "https://stackoverflow.com/questions/35413960/regular-expression-match-all-except-first-occurence" I'm wondering how to find the first occurence on a string only if it start's with a specfic character in PHP.

I would like to sanitize phonenumbers. Example bad phone number:

+49+12423#23492@aosd#+dasd

Regex to remove all "+" except first occurence.

\G(?:\A[^\+]*\+)?+[^\+]*\K\+

Problem: it should remove every "+" only if it starts with "+" not if the first occurence-position is greater than 1.

The regex to remove everything except numbers is easy:

[^0-9]*

But I don't know how to combine those two within one regex. I would just use preg_replace() twice.

Of course I would be able to use a workaround like if ($str[0] === '+') {...} but I prefer to learn some new stuff (regex :)

Thanks for helping.

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

0

You can use

(?:\G(?!\A)|^\+)[^+]*\K\+

See the regex demo. Details:

  • (?:\G(?!\A)|^\+) - either the end of the preceding successful match or a + at the start of string
  • [^+]* - zero or more chars other than +
  • \K - match reset operator discarding the text matched so far
  • \+ - a + char.

See the PHP demo:

$re = '/(?:\G(?!\A)|^\+)[^+]*\K\+/m';
$str = '+49+12423#23492@aosd#+dasd';
echo preg_replace($re, '', $str);
// => +4912423#23492@aosd#dasd
over 4 years ago · Santiago Trujillo Denunciar

0

You seem to want to combine the two queries:

A regex to remove everything except numbers

A regex to remove all "+" except first occurence

Here is my two cents:

(?:^\+|\d)(*SKIP)(*F)|.

Replace what is matched with nothing. Here is an online demo


  • (?:^\+|\d) - A non-capture group to match a starting literal plus or any digit in the range from 0-9.
  • (*SKIP)(*F) - Consume the previous matched characters and fail them in the rest of the matching result.
  • | - Or:
  • . - Any single character other than newline.

I'd like to think that this is a slight adaptation of what some consider "The best regex trick ever" where one would first try to match what you don't want, then use an alternation to match what you do want. With the use of the backtracking control verbs (*SKIP)(*F) we reverse the logic. We first match what we do want, exclude it from the results and then match what we don't want.

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