php - Regex Rules for First and Second Character - Stack Overflow

I need help on following regular expression rules of javascript and php.JSvar charFilter = new RegExp(&

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.

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.

Share asked Aug 13, 2010 at 7:44 knightriderknightrider 2,5837 gold badges30 silver badges42 bronze badges 1
  • 1 Note that [A|B] matches one of: A, | and B. The | does not mean OR inside a character class! – Bart Kiers Commented Aug 13, 2010 at 8:28
Add a ment  | 

4 Answers 4

Reset to default 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.):

= 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

  • regular-expressions.info
    • Lookarounds, Alternation, Anchors, Repetition, Dot, Character Class

Related questions

  • How does the regular expression (?<=#)[^#]+(?=#) work?

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

  • regular-expressions.info/Brackets for Grouping

Your OR is against the wrong grouping. Try:

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

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742405052a4437676.html

相关推荐

  • php - Regex Rules for First and Second Character - Stack Overflow

    I need help on following regular expression rules of javascript and php.JSvar charFilter = new RegExp(&

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信