Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!