4

I have a list of messages where I'm searching in that message for 4 or 3 digit number, I then replace it with that number.

So my current Regex is

Find

(.*)([0-9]{3,4})(.*)\r

Replace

\2

However, the problem is with [0-9]{3,4} only takes the first 3 digit if there are 4 digits, so even if there is a 4 digit number sequence, it will just grab a 3 digit number. Which is what I don't want.

Is there a way to make it grab a 4 digit number if it can and only grab a 3 digit number if it can't find the 4 digit number in that line.

Thanks

2 Answers2

2

Regular expressions have support for non-greedyness using the *? operator:

(.*?)([0-9]{3,4})(.*)\r

Depending on the program you use to match the regex, you will need to add additional flags.

In such case a regex is more happy to give a digit to the second group than to the first. By default regexes are greedy left-to-right: they will try to store everything in the first group and only move to the next if necessary (to still match the string).

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • Thanks for that it works. I didn't realise you make everything around it not greedy. – user3914104 Aug 06 '14 at 11:37
  • @CasimiretHippolyte: Thanks, modified, althoug semantically this doesn't change anything: it is already the last group thus it is by default already the less greedy one. – Willem Van Onsem Aug 06 '14 at 11:39
  • 1
    Semantically yes, but it may slow a little the search if the regex engine isn't enough smart to automatically optimize (in this case to make greedy) something like `.*?\r` *(<= and note that the `\r` is not always a part of a newline sequence)* – Casimir et Hippolyte Aug 06 '14 at 11:43
0

Try this one :

(([0-9]{4})|([0-9]{3}))

It will either take 4 digits, or 3 digits if it can't.

ChoiBedal
  • 111
  • 7