0 Replies Latest reply on May 29, 2007 3:33 AM by kelang

    Regular Expression help

    kelang
      hi there,
      i am new here, i got stuck with AS 3.0 regular expression class. Here is the problem

      i am trying to use RegExp to parse a math expression, here is part of the code for add&sub function


      var stringNum1 : String = "([-+]?\\d+\\.?\\d*)"; //standing for a regular number
      var parseAddSub1 : RegExp = new RegExp(stringNum1 + "([-+])" + stringNum1, "g");
      var parseExpression1:String="3-200*4*2+1000*2*1-10000-2";
      var result1:Object = parseAddSub1.exec(parseExpression1);
      while (result1 != null)
      {
      trace(result1);
      trace(parseAddSub1.lastIndex);
      result1 = parseAddSub1.exec(parseExpression1);
      }

      Str: 3-200*4*2+1000*2*1-10000-2
      results:
      0: 3-200
      1: 1+1000
      2: 1-10000

      But if I need to exclude any number with * as prefix or suffix
      so i give:
      for Prefix: var parseAddSub1 : RegExp = new RegExp("(?![/*^])"+"stringNum1 + "([-+])" + stringNum1, "g");
      this gives results:
      0: 3-200
      1: 1+1000
      2: 1-10000
      Obivously, this is not correct, only after I change
      var parseAddSub1 : RegExp = new RegExp("(?<![/*^])"+"stringNum1 + "([-+])" + stringNum1, "g");
      this gives results:
      0:3-200
      2:-10000-2

      Question: Does AS 3.0 really support "?<!", as far as I know and also you adobe claimed in AS help, only "?!" is supported
      anyway, the prefix has been solved

      Next, i am trying to figure out suffix,
      var parseAddSub1 : RegExp = new RegExp("stringNum1 + "([-+])" + stringNum1+"(?![/*^])", "g");
      i give results:
      0: 3-20 //hoho, funny right? but actually this is true
      1: 1+100
      2: 1-10000

      then, same as prefix, I change "(?![/*^])" into "(?<![/*^])", this gives
      0: 3-200
      1: 1+1000
      2: 1-10000

      so either works, so can anybody have some ideas?

      hoho, maybe too massy for you guys to read, but thanks in advance for your help or any comments