Skip to main content
added 27 characters in body; added 111 characters in body
Source Link
RobH
  • 17k
  • 6
  • 37
  • 72

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

Edit:

I think your regex needs to be more like: ^([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-[b|r])?(?:[1[br]{1}[0-9]?\\d*)?$ This makes everything following the first 3 numbers optional.

I do wonder whether a regex is the right option though... If you know the strings you are comparing are definitely well formed version numbers just do normal string manipulation.

// Pseudocode
String versionNumber = "1.1.1-r1";
int indexOfDash = versionNumber.indexOf("-");
if (indexOfDash != -1) 
{
    versionNumber = versionNumber.substring(0, indexOfDash);
}
String[] numbers = versionNumber.split(".");

// Now compare your numbers.

I generally like to avoid regular expressions as they are a lot more difficult to get right than people give them credit for. However, if you also need to check that it is a well formed version number you probably want to use the regex option.

In fact, I got the regex wrong - edited now. I changed the matching for the final part of the version number and added in the anchors.

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

Edit:

I think your regex needs to be more like: ([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-[b|r])?(?:[1-9]?\\d*) This makes everything following the first 3 numbers optional.

I do wonder whether a regex is the right option though... If you know the strings you are comparing are definitely well formed version numbers just do normal string manipulation.

// Pseudocode
String versionNumber = "1.1.1-r1";
int indexOfDash = versionNumber.indexOf("-");
if (indexOfDash != -1) 
{
    versionNumber = versionNumber.substring(0, indexOfDash);
}
String[] numbers = versionNumber.split(".");

// Now compare your numbers.

I generally like to avoid regular expressions as they are a lot more difficult to get right than people give them credit for. However, if you also need to check that it is a well formed version number you probably want to use the regex option.

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

Edit:

I think your regex needs to be more like: ^([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-[br]{1}[0-9]?\\d*)?$ This makes everything following the first 3 numbers optional.

I do wonder whether a regex is the right option though... If you know the strings you are comparing are definitely well formed version numbers just do normal string manipulation.

// Pseudocode
String versionNumber = "1.1.1-r1";
int indexOfDash = versionNumber.indexOf("-");
if (indexOfDash != -1) 
{
    versionNumber = versionNumber.substring(0, indexOfDash);
}
String[] numbers = versionNumber.split(".");

// Now compare your numbers.

I generally like to avoid regular expressions as they are a lot more difficult to get right than people give them credit for. However, if you also need to check that it is a well formed version number you probably want to use the regex option.

In fact, I got the regex wrong - edited now. I changed the matching for the final part of the version number and added in the anchors.

Edit following question update.
Source Link
RobH
  • 17k
  • 6
  • 37
  • 72

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

Edit:

I think your regex needs to be more like: ([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-[b|r])?(?:[1-9]?\\d*) This makes everything following the first 3 numbers optional.

I do wonder whether a regex is the right option though... If you know the strings you are comparing are definitely well formed version numbers just do normal string manipulation.

// Pseudocode
String versionNumber = "1.1.1-r1";
int indexOfDash = versionNumber.indexOf("-");
if (indexOfDash != -1) 
{
    versionNumber = versionNumber.substring(0, indexOfDash);
}
String[] numbers = versionNumber.split(".");

// Now compare your numbers.

I generally like to avoid regular expressions as they are a lot more difficult to get right than people give them credit for. However, if you also need to check that it is a well formed version number you probably want to use the regex option.

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.

Edit:

I think your regex needs to be more like: ([1-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-[b|r])?(?:[1-9]?\\d*) This makes everything following the first 3 numbers optional.

I do wonder whether a regex is the right option though... If you know the strings you are comparing are definitely well formed version numbers just do normal string manipulation.

// Pseudocode
String versionNumber = "1.1.1-r1";
int indexOfDash = versionNumber.indexOf("-");
if (indexOfDash != -1) 
{
    versionNumber = versionNumber.substring(0, indexOfDash);
}
String[] numbers = versionNumber.split(".");

// Now compare your numbers.

I generally like to avoid regular expressions as they are a lot more difficult to get right than people give them credit for. However, if you also need to check that it is a well formed version number you probably want to use the regex option.

Source Link
RobH
  • 17k
  • 6
  • 37
  • 72

It seems to me that you're using * when it looks like that's not the behaviour you want.

i.e. this bit -(b|r)* do you really mean a dash followed by either a 'b' or an 'r' zero or more times? I think it's more likely that you meant more along the lines of: there can be a dash followed by a 'b' or an 'r' but there doesn't have to be.

Do you want 1.1.1-brrrrrrrrrrr1 to match? What's the goal of the regex, is it just to see if it fits the pattern or do you want to use it to get the various numbers out?

Btw: If you don't care about groups you can use (?: ) instead of () to create non capturing groups.