Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

265
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda