First off using a regex to test a range is not the best application of a regular expression. It can be done but can get tricky.
As to your problem your mistake is that you are trying to put a pattern inside your qualifier. Qualifiers are part of the pattern they themselves don't contain any regex patterns. If you want to select 1 to 3 digits the pattern would be \d{1,3}
While that would match any of the values in your range it would also match any 3 digit value outside of your range. As I said before range matching is a poor application of regex because regular expressions have no concept of numbers. So you pattern has to match every string representation of the numbers in your range. That patterns can get pretty complex when your range endpoints don't end with a zero.
Below is a pattern for 0 to 255 (not 1 to 256)
Raw Match Pattern:
\b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b
Match Pattern Explanation:
The regular expression:
(?-imsx:\b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
2 '2'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[0-4] any character of: '0' to '4'
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
5 '5'
----------------------------------------------------------------------
[0-5] any character of: '0' to '5'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
1? '1' (optional (matching the most
amount possible))
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
\d? digits (0-9) (optional (matching the
most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
C#.NET Code Example:
using System;
using System.Text.RegularExpressions;
namespace myapp
{
class Class1
{
static void Main(string[] args)
{
String sourcestring = "source string to match with pattern";
Regex re = new Regex(@"\b(?:(?:2(?:(?:[0-4]\d)|(?:5[0-5])))|(?:1?\d\d?))\b");
MatchCollection mc = re.Matches(sourcestring);
int mIdx=0;
foreach (Match m in mc)
{
for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
{
Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
}
mIdx++;
}
}
}
}
$matches Array:
(
[0] => Array
(
[0] => 2
[1] => 1
[2] => 10
[3] => 35
[4] => 152
[5] => 193
[6] => 205
[7] => 251
)
)
Michael
"In theory, theory and practice are the same. In practice, they are not."
Albert Einstein