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

270
Vistas
How to extract middle values in regex

I am trying to extract the fifth and sixth value present in the stream through regex.

The stream is

12,097.00 435.00 100.00 43,037.00 3,090.00 200.00 86.00 45,890.47 7,570.00 51,514.47

I want values 200.00 and 100.00.

I tried ^(?:\S+\s+\n?){3,3} but it's selecting the string from beginning.

Can anybody help me please in getting the values that are present in the middle?

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

0

Certainly, if you have access to the code itself, it would be easier to split the string and get nth chunk by its index.

If you are limited to a regex, you can use

(?<=^(?:\S+\s+){2})\S+
(?<=^(?:\S+\s+){5})\S+

Or, if there can be leading whitespaces:

(?<=^\s*(?:\S+\s+){2})\S+
(?<=^\s*(?:\S+\s+){5})\S+

See a .NET regex demo.

Details:

  • (?<= - start of a positive lookbehind that requires the following sequence of patterns to appear immediately to the left of the current location:
    • ^ - start of string
    • \s* - zero or more whitespaces
    • (?:\S+\s+){2} - two occurrences of 1+ non-whitespace chars followed with 1+ whitespace chars
  • ) - end of the lookbehind
  • \S+ - one or more non-whitespace chars.
over 4 years ago · Santiago Trujillo Denunciar

0

Using a quantifier like {3,3} can be written as {3}, but note that in the example string the values 200.00 and 100.00 are not the 5th and the 6th value.

With your pattern you only get the values at the beginning as the anchor ^ asserts the start of the string.

To get the third and the sixth value, you could also use 2 capture groups by using a quantifier {2} for the parts in between.

^(?:\S+\s+){2}(\S+)(?:\s+\S+){2}\s+(\S+)
  • ^ Start of string
  • (?:\S+\s+){2} Repeat 2 times matching non whitespace chars followed by whitespace char
  • (\S+) Capture group 1, match 1+ non whitespace chars
  • (?:\s+\S+){2}\s+ Repeat 2 times matching whitespace chars and non whitespace chars
  • (\S+) Capture group 2, match 1+ non whitespace chars

Regex demo

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