2

I need help on following regular expression rules of javascript and php.

JS

var charFilter = new RegExp("^[A|B].+[^0123456789]$");

PHP

if (!preg_match('/^[A|B].+[^0123456789]$/', $data_array['sample_textfield'])) {

This regular expression is about

First character must be start with A or B and last character must not include 0 to 9.

I have another validation about, character must be min 3 character and max 6 number.

New rule I want to add is, second character cannot be C, if first letter is A.

Which means

ADA (is valid) ACA (is not valid)

So I changed the regex code like this

JS

var charFilter = new RegExp("^(A[^C])|(B).+[^0123456789]$");

PHP

if (!preg_match('/^(A[^C])|(B).+[^0123456789]$/', $data_array['sample_textfield'])) {

It is worked for first and second character. If i type

ACA (it says invalid) , But if i type AD3 (it says valid), it doesn't check the last character anymore. Last character must not contain 0 to 9 number, but it's show as valid.

Can anyone help me to fix that regex code for me ? Thank you so much.

knightrider
  • 2,533
  • 7
  • 30
  • 42

4 Answers4

3

Putting all of your requirements together, it seems that you want this pattern:

^(?=.{3,6}$)(?=A(?!C)|B).+\D$

That is:

  • From the beginning of the string ^
  • We can assert that there are between 3 to 6 of "any" characters to end of the string (?=.{3,6}$)
  • We can also assert that it starts with A not followed by C, or starts with B (?=A(?!C)|B)
  • And the whole thing doesn't end with a digit .+\D$

This will match (as seen on rubular.com):

= match =         = no match =
ADA               ACA
ABCD              AD3
ABCDE             ABCDEFG
ABCDEF
A123X
A   X

Note that spaces are allowed by .+ and \D. If you insist on no spaces, you can use e.g. (?=\S{3,6}$) in the first part of the pattern.

(?=…) is positive lookahead; it asserts that a given pattern can be matched. (?!…) is negative lookahead; it asserts that a given pattern can NOT be matched.

References

Related questions


On alternation precedence

The problem with the original pattern is in misunderstanding the precedence of the alternation | specifier.

Consider the following pattern:

this|that-thing

This pattern consists of two alternates, one that matches "this", and another that matches "that-thing". Contrast this with the following pattern:

(this|that)-thing

Now this pattern matches "this-thing" or "that-thing", thanks to the grouping (…). Coincidentally it also creates a capturing group (which will capture either "this" or "that"). If you don't need the capturing feature, but you need the grouping aspect, use a non-capturing group ``(?:…)`.

Another example of where grouping is desired is with repetition: ha{3} matches "haaa", but (ha){3} matches "hahaha".

References

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1

Your OR is against the wrong grouping. Try:

^((A[^C])|(B)).+[^0123456789]$
jasonbar
  • 13,333
  • 4
  • 38
  • 46
  • Hello, thanks for the code, i think it's somehow wrong, because, if i type, ACA (is invalid) and it's correct, but If i type ABC (is still showing as invalid, actually it should show valid) But if i type more than 3 characters, it's show valid. Any idea ? – knightrider Aug 13 '10 at 09:04
0

i think it should be like

/^(A[^C]|B.).*[^0-9]$/

try this test code

$test = "
    A
    B
    AB
    AC
    AAA
    ABA
    ACA
    AA9
    add more
";
$pat = '/^(A[^C]|B.).*[^0-9]$/';
foreach(preg_split('~\s+~', $test) as $p)
    printf("%5s : %s\n<br>", $p, preg_match($pat, $p) ? "ok" : "not ok");
user187291
  • 53,363
  • 19
  • 95
  • 127
0

In jasonbars solution the reason it doesn't match ABC is because it requires A followed by not C, which is two characters, followed by one or more of any character followed by a non number. Thus if the string begins with an A the minimum length is 4. You can solve this by using a look ahead assertion.

PHP

$pattern = '#^(A(?=[^C])|B).+\D$#';
Peter O'Callaghan
  • 6,181
  • 3
  • 26
  • 27