简体   繁体   中英

Regex Rules for First and Second Character

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.

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 eg (?=\\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

Your OR is against the wrong grouping. Try:

^((A[^C])|(B)).+[^0123456789]$

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");

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$#';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM