Reputation: 53
I am fairly new to using regex. I have a serial number which can take the following forms: VV-XXXXXX-P or VVXXXXXXP
If the hyphen variant is used, then the number of 'X' can be variable. For example 01-162-8 is equivalent to 010001628.
In order to identify the 2 formats, I have created the following regex's:
String HYPHENS = ([0]{1}[13]{1}-[1-9]{1,6}-[0-9]{1})
String NO_HYPHENS = ([0]{1}[13]{1}[0-9]{6}[0-9]{1})
However the issue with the NO_HYPHENS variant is that it allows 0 anywhere in the sequence For example: 010010628 should not be a valid sequence because there's a non leading 0.
Additionally, how would I create a regex that I can use to replace all 0 from the sequence but the first one? I have tried the following but it also replaces the first 0.
String code = 010001234;
code = code.replaceAll("0+", "");
How could I modify the regex's to achieve this?
Upvotes: 2
Views: 126
Reputation: 626794
You can use
String NO_HYPHENS = "0[13](?!0*[1-9]0)[0-9]{6}[0-9]";
code = code.replaceAll("(?!^)0(?!$)", "");
See the regex demo.
The 0[13](?!0*[1-9]0)[0-9]{6}[0-9]
regex matches:
0
- zero[13]
- one or three(?!0*[1-9]0)
- any zeros followed with a non-zero digit and then a zero is not allowed at this location[0-9]{6}
- six digits[0-9]
- a digit.I understand you use it in Java with .matches
, else, add ^
at the start and $
at the end.
Also, the (?!^)0(?!$)
regex will match any zero that is not at the string start/end position.
Upvotes: 2
Reputation: 173
^0[13]0*[1-9]*[0-9]$
^
- beginning of string
0
- first sign must be zero
[13]
- second sign must be one or three
0*
- sequence of zeros of variable length
[1-9]
- sequence of non-zeros of variable length
[0-9]
- finally one digit (it can be replaced with \d also)
$
- end of string
This regex has one problem: it doesn't check how many digits are in the XXXXXX section of serial number. But you can check it with length function:
String code = "010000230";
if (code.matches("^0[13]0*[1-9]*[0-9]$") && code.length() == 9) {
// code is valid
}
// replacement
code = code.replaceAll("^(0[13])0*([1-9]*[0-9])$", "$1$2");
Explanation of the replacement:
(0[13])
group number 1 (groups are in bracket)
0*
some zeros
([1-9]*[0-9])
group number 2
This will be replaced with:
$1$2
group number 1 and group number 2 ($1 means group number 1)
Upvotes: 1