Re: Regex - Ensure 0,1 occurrences from a list of possibilities

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hello Mark,

1[234] matches 12, 13, 14. It doesn't match 1222.

21 characters shorter with Jesse's suggestion (and even less pretty
;^)

"D-(1234|1243|1324|1342|1423|1432|2134|2143|2314|2341|2413|2431|
3124|3142|3214|3241|3412|3421|4123|4132|4213|4231|4312|4321|12[34]|
13[24]|14[23]|21[34]|23[14]|24[13]|31[24]|32[14]|34[12]|41[23]|42[13]|
43[12]| 1[234]|2[134]|3[124]|4[123]|[1234])$"

I find it more readable than ^D-(?:(?(1)(?!(?(1)))|(?<1>1))|(?(2)(?!(?(2)))|(?<2>2))|(?(3)(?!(?(3)))|(?<3>3))|(?(4)(?!(?(4)))|(?<4>4))){1,4}$

though... even though it's shorter...

But as I say as Regex teacher... Shorter is always better ;).

Jesse

-Mark

<nospam@invegat> wrote in message
news:nau0t4h2qm1kl7mi990r5uit0svnuk64f0@xxxxxxxxxx

of the 64 possible matches 40 would be incorrect for the pattern
1[234], for example 1222

On Mon, 30 Mar 2009 07:22:15 +0000 (UTC), Jesse Houwing
<jesse.houwing@xxxxxxxxxxxxxxxx> wrote:

Hello Mark,

There are only 64 permutations. Not a pretty regex though:

"D-(1234|1243|1324|1342|1423|1432|2134|2143|2314|2341|2413|2431|
3124|3142|3214|3241|3412|3421|4123|4132|4213|4231|4312|4321|123|
124|132|134|142|143|213|214|231|234|241|243|312|314|321|324|341|342
|
412|413|421|423|431|432|12|13|14|21|23|24|31|32|34|41|42|43|1|2|3|4
)$"

You could make that much shorter by making permutations like
([1234]|1[234]|2[134]...
etc, whould make it even shorter. There's even tools for that, but I
can't
find them right now. Used to use them to generate regex's for
SpamAssassin.
But I agree... not very pretty.

Jesse

-Mark

<nospam@invegat> wrote in message
news:vb20t4pe3tgjj7gm8igql0dcl9ec36e52p@xxxxxxxxxx
By my calculation there are 504 possibilities, I wouldn't call
that "only a few". If I were doing it these way I would generate
the matching strings with a 1 time use program.

On Sun, 29 Mar 2009 23:33:45 +0000 (UTC), Jesse Houwing
<jesse.houwing@xxxxxxxxxxxxxxxx> wrote:

Hello Byron,

I'm looking for an expression that checks that within a string
only
certain characters appear, and that they appear at most 1 time.
For example, a string can have up to 4 unique digits between 1
and
4.
The following strings should pass:
D-1
D-14
D-1234
D-41
The following should fail:
D- (no digit)
D-0 (out of range)
D-5 (out of range)
D-11 (repeated digit)
D-1 2 (embedded space)
D-12345 (out of range and too long)
The expression "D-[1234]{1,4}" comes close, but it matches even
when a
digit is repeated within the string.
Any help would be greatly appreciated.
As there's only a few possibilities, you could write them out
like this:

(1234|1324|1342|1423|1432|....4321)

And do the same with the other options.

A better wat woukd be to check the general format with a regex,
and then later (if you're using asp.net validators) check the
other requirement.

There are other possibilities (as invegat pointed out), but those
aren't
supported by clientside Javascript , which would require a
serverside
check
anyway.
--
Jesse Houwing
jesse.houwing at sogeti.nl


.



Relevant Pages

  • Re: Regex - Ensure 0,1 occurrences from a list of possibilities
    ... 21 characters shorter with Jesse's suggestion ... You could make that much shorter by making permutations like ... matching strings with a 1 time use program. ... D-11 (repeated digit) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex - Ensure 0,1 occurrences from a list of possibilities
    ... whould make it even shorter. ... matching strings with a 1 time use program. ... D-11 (repeated digit) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: reordering elements of a list
    ... greenflame wrote: ... elements and another list (which is may shorter, ... Also by the way the main list is always going to be a list of strings ... But you can also do it with list comprehension pretty easily, so this is probably just a shameless plug for NumPy :-) ...
    (comp.lang.python)
  • Re: Gregory Chaitins Famous Omega explicit implies only 1 in Millionth of Numbers of specific ( larg
    ... Then at least one shorter file has to map to a longer file. ... that would mean at least 50% of files would be impossible to compress ... shorten one of the string to 7 bits then at least 2 strings become 9 ... You could actually do this for any string length the tree you end up ...
    (comp.compression)
  • Re: Converting a number todate
    ... Microsoft MVP (Excel) ... struggling to find shorter and shorter formula solutions is ... that turns out to be 3 characters shorter than your posted (still using ...
    (microsoft.public.excel.programming)