1

Not sure what i am missing here. i have tried a few combinations what should work however ever time they are combines the last set of digits are drop.

I have also made the dash literal "-" to see if it help (Not)

Sample Data

PriceVolHistory_KITDQ_v1 (Range_0343935).xlsx
Amex_Robin Smyth (Range_0339935-0339982).pdf
0-CHIPS Participant List - 05-05-15 (Range_0339985-0339986).pdf
0-CHIPS Payment Message Key (Range_0339987-0339990).pdf
0-CHIPS Payment Message Structure (range_0339983-0339984).pdf
2610 Data (Range_0339991).xlsx

Regex

Range_(\d{7}|\d{7}-\d{7})
Range_(\d{7}|Range_\d{7}-\d{7})

Sample here - https://regex101.com/r/skjYvn/1

Any assistance is greatly apreaciated, thank you.

  • 1
    What program are you using? You appear to be using Perl regexes (or at least Perl Compatible), but most command line tools use Basic regexes (BREs) or Extended regexes (EREs), which are different from PCREs. – Wildcard Jul 27 '17 at 23:37
  • Using Notepad++ , i get the same results here . regex101.com/r/skjYvn/1 – user68650 Jul 27 '17 at 23:38
1

Regexes are greedy. They will match the first thing they can. (|a|b) will always match a null string.

Similarly, (\d{7}|\d{7}-\d{7}) will always match just \d{7} (or not match at all). It will never proceed to the second option.

What you want instead is (\d{7}-\d{7}|\d{7}). But why be so verbose?

\d{7}(-\d{7})? will work just as well.

So you get:

Range_\d{7}(-\d{7})? 
  • This is not the first time i have forgotten the GREEDYness of Regex! i did not think of it since i was specifying both term repeatedly! Thank you for the assistance! much appreciate. – user68650 Jul 27 '17 at 23:49
  • i'd say the problem in the question is an example of regexes being lazy, not greedy. laziness = matching the first thing they can (i.e short-circuit evaluation), greediness = matching everything that they can (and using that greediness is part of the solution to the problem). also, laziness overrules greediness. – cas Jul 28 '17 at 5:29

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.