View Single Post
Old May 13th, 2006, 7:48 AM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
You can create "optional" matches. I can't quite understand your example, so I am making my own. Plus you seem to be doing odd things with character classes.

$string="123456789"
$string2="123456"
$string3="12345"
$pattern="^[0-9]{5}[0-9]?$"
The ? mark denotes an optional numeric character class. This pattern finds "whole variables" that are either 5 or 6 numeric characters long. So, pattern will match string2 and string3.

What exactly are you trying to do? You can get "partial" matches with optional lookaround. The best approach when you have a hashed up situtation in regular expressions is to provide for yourself:

1. a clear set of examples for yourself that both match and do not match.
2. Derive these examples from from your known data universe.
3. Write a clear (English, Dutch, whatever) sentence that sets down what you're trying to do.

Go from there. Test your pattern against a bunch of trial data items.
jim mcnamara is offline   Reply With Quote