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

198
Views
Repeated group captures more matches than specified

I am trying to match the following sequence, line by line:

  • start of line
  • maybe some space
  • the Kd string
  • at least one space, maybe more
  • either 3 or 4 float numbers whose format may be messy
  • maybe some space
  • end of line

The problem is that the 4th sample is also captured even though it has 5 numbers in it.

Pattern:

^\s*Kd\s+.*(?:[-+]?0*\d*\.?\d*){3,4}$

Samples:

Kd   1.0  0.1   0.0
   Kd   .0  4.   01.
  Kd   .0  4.   01.  01.
 Kd   .0  4.   01. 01. 01.
    Kd   1.0  0.1   0.0  0.0
  Kd   1.0  0.1   0.0

Expected captures:

  • 1.0, 0.1, 0.0
  • .0, 4., 01.
  • .0, 4., 01., 01.
  • failure
  • 1.0, 0.1, 0.0, 0.0
  • 1.0, 0.1, 0.0

Question:

What am I doing wrong in the regex so it also matches lines with more than 4 floats in them?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The main problem is the .* part that matches any zero or more chars other than an LF char, as many times as possible. You also need to put \s+ into the repeated group so as to allow whitespaces in between the numeric values.

You can use

^\s*Kd(?:\s+([-+]?(?:\d*\.?\d+|\d+\.\d*))){3,4}$

See the .NET regex demo. Details:

  • ^ - start of string
  • \s* - zero or more whitespaces
  • Kd - a fixed string
  • (?:\s+([-+]?(?:\d*\.?\d+|\d+\.\d*))){3,4} - three to four occurrences of
    • \s+ - one or more whitespaces
    • ([-+]?(?:\d*\.?\d+|\d+\.\d*)) - Group 1:
      • [-+]? - an optional - or +
      • (?:\d*\.?\d+|\d+\.\d*) - either zero or more digits, an optional . and one or more digits, or one or more digits, . and zero or more digits
  • $ - end of string.
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!