-1

I'm looking for a regex for Invoice Number in Vbscript It can have alphanumeric but at least one numeric digit is a must.

I'm using the below regex but it matches ALPHA String INVOICE also. It need to have at least one digit

\b(?=.*\d)[A-Z0-9\-]{5,12}\b

Expected Match String

1233444
M62899M
M828828
783838PTE
A751987

Expected Unmatch String

INVOICE
ubb62727
XYZ
123

If we use ([A-Z0-9]*[0-9]+[A-Z0-9]*), I can't specify the length.

Please suggest a proper regex. Please note its totally different from the suggested duplicate as the requirement, format is different.

A.W
  • 35
  • 1
  • 8
  • Add a positive lookahead with a beginning-of-string anchor (`^` or `\A`): `^(?.*\d)`. – Cary Swoveland Jul 21 '20 at 06:18
  • 2
    Have a look at [this](https://regex101.com/r/w0kyNL/1) – JvdV Jul 21 '20 at 06:24
  • 2
    @Lankymart The requirements in the proposed duplicate are quite different. – tripleee Jul 21 '20 at 08:35
  • 1
    @tripleee The requirements might be, but the implementation (how you group digits exclude certain characters etc is exactly the same). I'm not saying it's a solution but it shows how they could structure the RegEx to get the output they require. The principles are exactly the same, both are trying to identify invoice numbers (The nature of an Invoice Number means they won't be exactly the same). – user692942 Jul 21 '20 at 08:41
  • Does this answer your question? [Regular expression for Invoice Number](https://stackoverflow.com/a/34642039) – user692942 Jul 21 '20 at 09:05
  • Does this answer your question? [Reference - What does this regex mean?](https://stackoverflow.com/q/22937618) – user692942 Jul 21 '20 at 09:06
  • 1
    The language tag is appropriate here. It provides context for which regex engine the asker is using and it tell answerers what language the expected solution should be in. Please cease and desist from any further rollbacks. If you want to discuss this further, take it to [Meta]. – Cody Gray - on strike Jul 21 '20 at 22:29
  • https://regular-expressions.mobi/vbscript.html?wlr=1 – user692942 Jul 22 '20 at 00:27

1 Answers1

1

The blanket .* in your lookahead will happily skip past the trailing \b if it has to. Make it more constrained, so it can't.

\b(?=[-A-Z]*\d)[A-Z0-9-]{5,12}\b

(I removed the backslash before the -; if you really want to allow a literal backslash, obviously add it back, to the character class in the lookahead also. A dash at beginning or end of a character class is unambiguous and doesn't require a backslash escape; this is also the only way to have a literal dash in a character class in many regex dialects.)

tripleee
  • 175,061
  • 34
  • 275
  • 318