in reply to Regex: Specifying quantity for bracketed items

Maybe you want something like this:

/ ^(?![ ]) # Doesn't start with a space .*? # Anything [a-z0-9]{5,12} # Has a 5..12 lowercases or digits in a row .* # Anything (?<![ ])$ # Doesn't end with a space /x

which will match
a1b2c3
BVBDa1b2c3
a1b2c3ZZZ
!@#a1b2c3$%^
! a1b2c3 FF

Replies are listed 'Best First'.
Re^2: Regex: Specifying quantity for bracketed items
by Eimi Metamorphoumai (Deacon) on Nov 10, 2004 at 22:07 UTC
    Actually, that doesn't limit you to 5..12, that matches anything with at least 5 lowercases or digits in a row. Any more than 12 just get sucked into the .* after it. Which makes it equivalent to
    / ^(?![ ]) # Doesn't start with a space .*? # Anything [a-z0-9]{5} # Has 5 lowercases or digits in a row .* # Anything (possibly more lowercases or digits) (?<![ ])$ # Doesn't end with a space /x

      Right you are! Fixed below.

      / ^(?![ ]) # Doesn't start with a space .*? # Anything (?<![a-z0-9]) # For the {5,12} to be meaningful. [a-z0-9]{5,12} # Has a 5..12 lowercases or digits in a row (?![a-z0-9]) # For the {5,12} to be meaningful. .* # Anything (?<![ ])$ # Doesn't end with a space /x