PDA

View Full Version : ^ and $ in single line regular expression?


slmille4@gmail.com
11-18-2008, 12:59 AM
I'm trying to come up with a regular expression that can recognize a positive or negative decimal, and I've been able to come up with one that works in other programs:

^\-?\d*\.?\d*$

The problem is that the input needs to be in a single line TextInput component, and apparently Flex will only process the ^ and $ characters with multi-line input. Am I missing something? Is there a better expression for recognizing a decimal in Flex?

drkstr
11-18-2008, 01:45 AM
RegExp is done on Strings, it has nothing to do with the amount of lines. Your expression seems fine to me, how are you using it exactly?

I ran a quick test on it and here were my results:

var test:String;
test = "12.334555";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "-15";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "-12.334";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "6a";
trace(test.match(/^\-?\d*\.?\d*$/));

outputs:
12.334555
-15
-12.334
null

Best Regards,
~Aaron

slmille4@gmail.com
11-18-2008, 03:41 AM
RegExp is done on Strings, it has nothing to do with the amount of lines. Your expression seems fine to me, how are you using it exactly?

I ran a quick test on it and here were my results:

var test:String;
test = "12.334555";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "-15";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "-12.334";
trace(test.match(/^\-?\d*\.?\d*$/));
test = "6a";
trace(test.match(/^\-?\d*\.?\d*$/));

outputs:
12.334555
-15
-12.334
null

Best Regards,
~Aaron

Whoops, I was using it to process the result from a TextInput field and I completely misunderstood how the event worked. Here's what I ended up with after I realized my mistake (and if anyone is interested, this seems to match how Flash checks the xscale/yscale etc input fields)

var re:RegExp = /^\-?\d*\.?\d*$/;
var newStr:String = event.target.text.substring(0,event.target.selecti onBeginIndex)+event.text+event.target.text.substri ng(event.target.selectionEndIndex)
if (!re.test(newStr))
event.preventDefault();