0

I'm trying to make it so that the following are able to fit into the regex, but I'm not able to make it work.

I'm using js on regex101 for this.

These should pass

  • 3
  • 3.4
  • 3.5.3
  • 2.5.*
  • 3.*

These should fail

  • 33.2
  • 2.
  • 3.1.2.
  • .2
  • 3.1.2.....

My current regex is \d?[\.?]? This only approves 3. I try going forward and use \d?[\.?\d?]? but it still only allows 3.

Edit: 3.5 technically passes in the way that 3. and 5 pass individually but not together. How can I make it together?

Why isn't it working on the rest of the numbers? And how do I fix it?

Update: Please explain how the regex does work if you manage to find it. Thank you.

12944qwerty
  • 2,001
  • 1
  • 10
  • 30

4 Answers4

0

You can use ^(\d+\.)?(\d+\.)?(\*|\d+)$ as found in this answer for version numbering regex.

If you want only a singular digit between the dots then remove the + symbols resulting in:

^(\d\.)?(\d\.)?(\*|\d)$

Matt Davis
  • 1,167
  • 8
  • 21
  • This regex code appears to have the same problem as I stated above. (but I want it to match the full numbers. For example: `3.5` matched, but it was matched like `3.` and `5`) – 12944qwerty Sep 18 '20 at 22:34
  • They're all still part of match 1. Yes `3.` is match 1 capturing group 1 and `5` is match 1 capturing group 2, but the regex results in a single match. – Matt Davis Sep 18 '20 at 22:36
  • Since he doesn't need to capture the different parts, use non-capturing groups to make it clearer. – Barmar Sep 18 '20 at 22:38
  • ^ `^(?:\d+\.)?(?:\d+\.)?(?:\*|\d+)$` or shortened `^(?:\d+\.){0,2}(?:\*|\d+)$` The single digit version can be further shortened to `^(?:\d\.){0,2}[*\d]$`. – 3limin4t0r Sep 18 '20 at 22:44
0

^\d(\.\d)*(\.\*)?$

var values=[
  "3", "3.4", "3.5.3", "2.5.*", "3.*",
  "33.2", "2.", "3.1.2.", ".2", "3.1.2."
];

values.forEach(value=>
  console.log(
    value,
    value.match(/^\d(\.\d)*(\.\*)?$/)?"Pass":"Fail"
  )
);

function test() {
  var value=document.getElementById("input").value;
  document.getElementById("result").innerText=
    value.match(/^\d(\.\d)*(\.\*)?$/)?"Pass":"Fail";
}
<input id="input" onchange="test()">
<button onclick="test()">Test</button>
<span id="result"></span>

Must start (^) with a single digit (\d).
May be followed by unlimited pairs of period+single digit ((\.\d)*) - pair: (\.\d), unlimited: *.
May be followed by a single pair of period+asterix ((\.\*)?) - pair: (\.\*), may(single/none): ?.
And end: $.

Should you want a non-zero digit, replace all ds with [1-9].

Should you want a number rather than a digit, add a '+' after each d: ^\d+(\.\d+)*(\.\*)?$.
And without leading zeros: ^[1-9]\d*(\.[1-9]\d*)*(\.\*)?$.

iAmOren
  • 2,760
  • 2
  • 11
  • 23
0

(?:\d\.) -> Non capturing group, with Digit \d matching 0-9. Escaped decimal matches a decimal.

? -> followed by a ? which matches 0 to 1 of the preceding token...

(?:\d\.) -> Non capturing group, with Digit \d matching 0-9. Escaped decimal matches a decimal.

? -> followed by a ? which matches 0 to 1 of the preceding token...

(?:\d) -> Non capturing group, with Digit \d matching 0-9.

$ ends string

NOTE: This will allow 0.*, *.0, 0.0.*, 0.* and 0,0,0 where * = some single digit number... If you wish to filter out zeros as null then more logic would be...

let regEx = /^(?:\d\.)?(?:\*|\d\.)?(?:\*|\d)$/gm;

let el = document.getElementById('input');

el.addEventListener('keyup', function(){
  let thisVal = this.value;
  
  if(thisVal.match(regEx)){
    console.log(thisVal + " is allowed")
  }else(
    console.log(thisVal + " is NOT allowed")
  )
})
Enter a string <input id="input">
dale landry
  • 7,831
  • 2
  • 16
  • 28
0

You could match a digit followed by an optional decimal part and an optional part to match a dot followed by either an asterix or a digit.

To prevent partial matches you can use anchors to assert the start ^ and the $ end of the string.

^\d(?:\.\d)?(?:\.[*\d])?$

Explanation

  • ^ Start of string
  • \d Match a digit
  • (?:\.\d)? Optionally match a dot followed by a digit
  • (?:\.[*\d])? Optionally match a dot and either a * or digit
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70