List Info

Thread: Help checking for character NOT repeated




Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 05:39:17
I'm trying to create a simple validation but struggling to
see how to
do duplicate checking.

A field can contain 2 or 3 characters from the set C, E, X
The letters can appear in any order
There can be no duplication so CXX or ECE are not valid

I know there only 12 possible options in this particular
example, but
there may be more characters allowed in future.

Is theere a nice neat way to use regexps to validate? I was
looking at
back references but discovered you can't use back refs in a
character
class and certainly can't negate them.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 08:32:58
^(([CEX])(?!2)){2,3}$

The most recent capture against the character class will be
stored in
$2; this capture is followed by the assertion, "Make
sure that the
next character isn't the same as $2."


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 16:18:45
On Feb 16, 9:32 am, "SlideGuitar" <d...anthonynassar.com> wrote:
> ^(([CEX])(?!2)){2,3}$
>
> The most recent capture against the character class
will be stored in
> $2; this capture is followed by the assertion,
"Make sure that the
> next character isn't the same as $2."

Hi, Tony:

This pattern may miss out something like:  DED, so I guess,
you need
to add one more option:

  ^(([CEX])(?!2|.2)){2,3}$

Regards,
Xicheng 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 17:12:27
On Feb 16, 6:39 am, "Neff" <fgpsm...gmail.com> wrote:
> I'm trying to create a simple validation but struggling
to see how to
> do duplicate checking.
>
> A field can contain 2 or 3 characters from the set C,
E, X
> The letters can appear in any order
> There can be no duplication so CXX or ECE are not
valid
>
> I know there only 12 possible options in this
particular example, but
> there may be more characters allowed in future.
>
> Is theere a nice neat way to use regexps to validate? I
was looking at
> back references but discovered you can't use back refs
in a character
> class and certainly can't negate them.

One of my reply to your post seems missing, so I will
re-post another
way for your problem.

In fact, it's much easier to check duplication, for
example:

   ([CEX])(?=.*1)

returns TRUE if there is any duplication of chars in [CEX]

Now you want:
1) no any dulicated char in [CEX]
2) exact 2 or 3 chars
3) no any chars in [^CEX]

Let's look at it the other way, negating the constraints
1) any duplicate char in [CEX] exists   ([CEX])(?=.*1)
2) or any char in [^CEX] exists         ^(?=.*[^CEX])
3) has 0 or 1 char in [CEX]             ^[CEX]?$
4) has more than 3 chars                ^[CEX]{3,}$

(4) can be skipped coz if it's TRUE, either (1) or (2) must
be TRUE.

so we can group the above pattern into one, like:

  ^[CEX]?$|^(?=.*[^CEX])|([CEX])(?=.*1)

This is the negation of your needs. use a negation
operator(i.e. 'not'
in Perl) on the above pattern, i.e. a Perl test code under
Linux box
can be like:

echo 'KCXE
XEE
XEC
XXEF
C
EXCC
CE' | perl -lne '
    print "matched: $_" if not m{
       ^[CEX]?$|^(?=.*[^CEX])|([CEX])(?=.*1)
    }x'
matched: XEC
matched: CE

It's not hard to extend this into more than 3 char-class. I
may missed
out something in the above logics, please check them out by
yourself.

Regards,
Xicheng


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 17:15:55
On Feb 16, 5:18 pm, "Xicheng Jia" <xich...gmail.com> wrote:
> On Feb 16, 9:32 am, "SlideGuitar"
<d...anthonynassar.com> wrote:
>
> > ^(([CEX])(?!2)){2,3}$
>
> > The most recent capture against the character
class will be stored in
> > $2; this capture is followed by the assertion,
"Make sure that the
> > next character isn't the same as $2."
>
> Hi, Tony:
>
> This pattern may miss out something like:  DED, so I
guess, you need

sorry for the typo, should be:

    strings like 'XEX' instead of 'DED' may be validated

Regards,
Xicheng


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-16 18:52:41
Good catch, Xicheng; I didn't read the original posting
carefully
enough, and didn't see that *no* duplication is allowed.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Help checking for character NOT repeated
country flaguser name
United States
2007-02-19 03:41:15
Thanks for all your help folks - I think I've got it all
working now.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-7]

about | contact  Other archives ( Real Estate discussion Medical topics )