-1

I have the following regex for a string which starts by a + and having numbers only:

PatternArticleNumber = $"^(\\+)[0-9]*";

However this allows strings like :

+454545454+4545454

This should not be allowed. Only the 1st character should be a +, others numbers only. Any idea what may be wrong with my regex?

refresh
  • 1,319
  • 2
  • 20
  • 71

1 Answers1

2

You can probably workaround this problem by just adding an ending anchor to your regex, i.e. use this:

PatternArticleNumber = $"^(\\+)[0-9]*$";

Demo

The problem with your current pattern is that the ending is open. So, the string +454545454+4545454 might appear to be a match. In fact, that entire string is not a match, but the engine might match the first portion, before the second +, and report a match.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360