Close, not quite.  The "quantifier" (i.e. ?,+,*) appear AFTER the "atom"
(i.e. char or special symbol).

The syntax is also off a bit.  It should be =~ and /../ (not single ticks).

$result =~ /^ *-?[0-9]+[.]?[0-9]* *\$/; 

^ = beginning of line (also called an anchor)

 * = zero or more spaces (not whitespace).  I suggest using \s* instead,
which is zero or more whitespace (includes spaces, tabs, returns, and
newlines in some situations)

-? = Matches an optional "-" char (0 or 1 times).

[0-9]+ =  1 or more numbers (use \d+ instead)

[.]? = optional char (any char).  Brackets not needed, use .? instead.

[0-9]* = 0 or more digits, again use \d* instead.

 * = 0 or more spaces, again use \s* instead.

\$ = matches an actual "$" char.

So the new regex looks like this...

$result =~ /^\s*-?\d+.?\d*\s*\$/;

Or using the "x" modifier (a good thing to do) to space it out, and add
comments... 

$result =~ /
  ^    # start of string
  \s*  # 0+ whitespace
  -?   # 0-1 "-"
  \d+  # 1+ digits
  .?   # 0-1 any char
  \d*  # 0+ digits
  \s*  # 0+ whitespace
  \$   # "$" char
/x;


The regex is starting to look like it is supposed to match numbers like
21.25$ and -50.00.  ...In which case the ".?" is supposed to be a real
period, not an "any char".  If it should be a real period (or decimal
point), use "\.?" instead.

Rob


-----Original Message-----
From: Johnson, Shaunn [mailto:[EMAIL PROTECTED]
Sent: Thursday, October 02, 2003 5:20 PM
To: [EMAIL PROTECTED]
Subject: explain regex statement?


Howdy:

I have a statement and I'm trying to see if
I understand what it is really is doing (not my code).

The code does this:

[snip]

result ~ '^ *-?[0-9]+[.]?[0-9]* *\$' 

[/snip]

I break out the meaning as this:


[snip example]
^ = beginning of line plus a white space
*- = everything up to, and including,  to the ' - '
?[0-9] =  1 or 0 times of a number
+[.] = 1 or more times any character
?[0-9]* *\$ = 1 or more times any number plus any other character and
            a white space and then any MORE characters to the 
            end of the line

[/snip example]

How far off am I?

Thanks!

-X

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to