Raheel Khan
Raheel Khan

Reputation: 14787

RegEx issue for custom barcode value

I am trying to parse the following string and can't get the regex quite right even though it is pretty basic. I think I have the optional grouping syntax wrong. The data can be one of the following:

PV{X}
PV{X.X}
PV{X.X.X}
PV{X.X.X.X}

using:

^PV\{\d+(\.\d+){0, 3}\}$

where:

Upvotes: 0

Views: 202

Answers (2)

gpmurthy
gpmurthy

Reputation: 2427

Consider the following Regex...

PV{\d{1,4}(.\d{1,4})*}

Upvotes: 0

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Okay, so this Regex will do it for you:

PV\{(\d+?(?:\.\d+){0,3})\}

And here is a Regex 101 to prove it.

The differences?

First, you had {0, 3} and so it thought that was a literal value to match. You just needed to get rid of that {space} in there. Next, the optional groups, that could occur 0 - 3 times, I dropped a ?: in there so it doesn't actually capture that group. And then finally, I wrapped the actual value inside the { } so that it would group that. You may want to change your groupings some, but this will surely match the entire string for you when appropriate.

Upvotes: 4

Related Questions