Got more questions? Find advice on: ASP | SQL | XML | Windows
Welcome to RegexAdvice Sign in | Join | Help

Trying to capture a series of values.

  •  07-13-2010, 5:00 PM

    Trying to capture a series of values.

    Howdy all,

    I've got this incoming string "132 / 456 / 789" (Or sometimes even "123/456/789" but it should always be with / seperating the numeric values.) and I'm trying to capture just the numbers and move them into a variable.  My RegEx is: 

    ([0-9]+)(?:[^0-9]*)([0-9]+)(?:[^0-9]*)([0-9]+)(?:[^0-9]*)

    So, capture a set of numbers, don't capture a set of non-numbers, repeat 3 times.  This works great if there are exactly 3 sets of numbers.

    Here's my test BLOCKED SCRIPT

     

     

    var testVar = "123 / 456 / 789";
    var reg_exp1 = /([0-9]+)(?:[^0-9]*)([0-9]+)(?:[^0-9]*)([0-9]+)(?:[^0-9]*)/;
    var testVarReg1 = testVar.replace(reg_exp1, "$1");
    var testVarReg2 = testVar.replace(reg_exp1, "$2");
    var testVarReg3 = testVar.replace(reg_exp1, "$3");

    document.write("Capture1: " + testVarReg1);
    document.write(" Capture2: " + testVarReg2);
    document.write(" Capture3: " + testVarReg3);

     

     

     

    This produces: "Capture1: 123 Capture2: 456 Capture3: 789" which is exactly what I want.

     

    The problem is sometimes there will only be 2 numbers.  Such as "123 / 456"  This causes weird things to happen.

    Only 2 numbers produces: Capture1: 123 Capture2: 45 Capture3: 6

     

    Any suggestions on how to fix this?  Is there an easier way to accomplish this?

     

    Thanks,

View Complete Thread