简体   繁体   中英

Create regular expression match exact word with space followed by string length 8

I am very new to regular expressions , I want to check in a sentence that matches with exact given word followed by space and a alphanumeric string starts with number and length of that string should be 8.From the below expression i am getting 1 as result and matches PO. But I am not getting how to create the rule to match PO followed by space and starts a string with first letter numeric and length with 8 characters.Any help would be greatly appreciated.

$header = 'Amazon.fr PO 2YCLN4EM';
$k = preg_match("/[PO]*/", $header);
echo $k;

Try as follows

Use \\d and Limiting Repetation for match exact 7 times. DEMO

/PO\s\d\w{7}/
$header = 'Amazon.fr PO 2YCLN4EM';

if (preg_match('/PO\ \d\w{7}/', $header, $matches)) {
    print_r($matches);
}

Note that the function only returns whether you have a match or not. The third parameter contains the matches.

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