关于公式和字符的preg_match给出了什么?

时间:2016-04-05 15:40:12

标签: php regex preg-match

我需要能够判断出是否存在符合以下条件的连续剧匹配:

void swap(struct node *head)
{
    struct node *p, *a, *q;
    p = head;
    do{
        head = p;
        p = head -> next -> next;
        q = head->next;               
        head -> next -> next = head;
        head -> next = p;
        head = q;
    } while(p!=NULL);
}

因此,给定以下#include <stdio.h> #include <stdlib.h> #include <conio.h> struct node { int dat; struct node *next; }; void print(struct node *); void swap(struct node *); int main() { int i; struct node *head, *h1, *h2, *h3, *h4, *h5, *h6, *h7, *h8; h1 = (struct node *)malloc(sizeof(struct node)); h2 = (struct node *)malloc(sizeof(struct node)); h3 = (struct node *)malloc(sizeof(struct node)); h4 = (struct node *)malloc(sizeof(struct node)); h5 = (struct node *)malloc(sizeof(struct node)); h6 = (struct node *)malloc(sizeof(struct node)); h7 = (struct node *)malloc(sizeof(struct node)); h8 = (struct node *)malloc(sizeof(struct node)); head = (struct node *)malloc(sizeof(struct node)); head = h1; h1 -> dat = 1; h1 -> next = h2; h2 -> dat = 2; h2 -> next = h3; h3 -> dat = 3; h3 -> next = h4; h4 -> dat = 4; h4 -> next = h5; h5 -> dat = 5; h5 -> next = h6; h6 -> dat = 6; h6 -> next = h7; h7 -> dat = 7; h7 -> next = h8; h8 -> dat = 8; h8 -> next = NULL; print(head); printf("\n\n"); swap(head); getch(); return 0; } void print(struct node *head) { if(head != NULL) { printf("%d\n", head -> dat); head = head -> next; print(head); } } void swap(struct node *head) { struct node *p, *a, *q; p = head; do{ head = p; p = head -> next -> next; q = head -> next; head -> next -> next = head; head -> next = p; head = q; } while(p != NULL); } 数组,如何使用指定的公式为与$formula = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX'; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $serials = array( '9876-345-ABC', '7856Y-YURYW-00UEW-YUI23-YYYYY', '0934Y-R6834-27495-89999-11123' ); 中的任何字符匹配的所有值返回true,其中$serials是任何字符的占位符在$chars内。但我还需要确保公式中的连字符位于给定序列号的正确位置。

X

应该在$chars的最后2个元素上找到回声。看起来很简单,但无论我怎么努力,我仍然无法绕过正则表达式。

5 个答案:

答案 0 :(得分:2)

当然不是最好的,但请试一试并发表评论

假设: - formula仅包含X's

$formula = 'XXX-XX-XXX-X-XXXXX';
$parts = split("\-", $formula);

$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$reg = '';

foreach ($parts as $x) {
    $reg = $reg . "" . '[' . "" . $chars . "" . ']{' . "" . strlen($x) . "" . "}" . "" . "-";
}

$reg = substr_replace($reg, '', -1);

$serials = array(
    '9876-345-ABC',
    '7856Y-YUR-00W-YUI23-YYY',
    '0934Y-R6834-27495-89999-11123',
    'XXX-XX-XXX-X-XXXXX'
);

$reg = '/^' . "" . $reg . "" . '$/';;

foreach($serials as $serial) {
    if(preg_match($reg, $serial) != 0) {
        echo $serial;
        echo "\n";
    }
}

<强> Ideone Demo

答案 1 :(得分:1)

$formula = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$serials = array(
    '9876-345-ABC',
    '7856Y-YURYW-00UEW-YUI23-YYYYY',
    '0934Y-R6834-27495-89999-11123'
);

foreach($serials as $serial) {
    $str  = str_replace(str_split($chars), 'X', $serial);
    echo $str == $formula ? "yes" : "no";
}    

答案 2 :(得分:0)

你可以去(multiline mode):

^(?:[0-9A-Z]{3,5}-?){3,5}$
# match the start of the line
# open a non-capturing group (?:
# look for a digit (0-9) or an uppercase letter (A-Z)
# ... between 3-5 times
# make the dash optional -?
# and repeat the non-capturing group 3-5 times
# $ makes sure this is the end of the string

由于精彩的regex101.com目前似乎不起作用,这里是正则表达式的非图形化示例。它将匹配末尾带星号的那些:

9876-345-ABC *
7856Y-YURYW-00UEW-YUI23-YYYYY *
0934Y-R6834-27495-89999-11123 *
this-one-not
this one neither

翻译为PHP,这将是:

$regex = '~^(?:[0-9A-Z]{3,5}-?){3,5}$~';
if (preg_match($regex, $string)) {
    echo "This is a valid serial";
}

答案 3 :(得分:0)

你可以这样做,它会在连续的X上使用“{}”。

/**
* $formula = 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX';
* $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
* $serials = array(
*    '9876-345-ABC',
*    '7856Y-YURYW-00UEW-YUI23-YYYYY',
*    '0934Y-R6834-27495-89999-11123'
* );
*/
function checkThisFormula($formula, $chars, array $serials) {
    $formulaLength = strlen($formula);
    $regex = "";
    $charsRegex = "[".$chars."]";
    $lastIsX = false;
    $nbX = 0;
    // let's construct the regex from formula
    for($i = 0; $i < $formulaLength; $i++) {
        if($formula[$i] === 'X') {
            // let's count how many X we see before writing
            $nbX++;
            $lastIsX = true;
        } else {
            if($lastIsX) {
                // end of successive Xs
                $regex .= "[".$chars."]";
                if($nbX > 1) {
                    $regex .= "{".$nbX."}";
                }
                // reinit X count
                $lastIsX = false;
                $nbX = 0;
            }
            // have to be this exact char
            $regex .= '\\'.$formula[$i];
        }
    }
    if($lastIsX) {
        // if the last char is an X, have to write it too !
        $regex .= "[".$chars."]";
        if($nbX > 1) {
            $regex .= "{".$nbX."}";
        }
    }
    // let's make the regex with flag for case insensitive
    $regex = "#".$regex."#i";
    $result = array();
    // let's loop on every serial to test it
    foreach($serials as $serial) {
        $result[$serial] = preg_match($regex, $serial);
    }
    return $result;
}

输出:

Array
(
    [9876-345-ABC] => 0
    [7856Y-YURYW-00UEW-YUI23-YYYYY] => 1
    [0934Y-R6834-27495-89999-11123] => 1
)

答案 4 :(得分:0)

我认为简单的方法就是这样:

folly::uriEscape

结果将是:

found - 7856Y-YURYW-00UEW-YUI23-YYYYY
found - 0934Y-R6834-27495-89999-11123

我希望你想做的事。