2 回答

TA貢獻1886條經驗 獲得超2個贊
我會使用這個模式:(?<=\bclaim code: )\S+
$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";
preg_match_all("/(?<=\bclaim code: )\S+/", $message, $matches);
print_r($matches[0][0]);
這打?。?/p>
NE7N-TUD6NV-GUAB
您當前方法的主要問題是正則表達式模式本身。 匹配零個或多個空格字符,您應該像我上面使用的那樣使用。\s*\S*

TA貢獻1820條經驗 獲得超10個贊
你可以使用積極的觀察來實現這個目標。我建議使用,除非你有多個索賠代碼。因為內存消耗比 .對于您的解決方案,請查看以下代碼:preg_matchpreg_match_all()preg_match()
$message = "Hurray! You've received a $100 Amazon Gift Card. Hope you enjoy this Amazon Gift Card! What's next? Apply the gift card to your Amazon account:=20 https://www.amazon.com/g/ Don't have an Amazon account? Sign up to redeem: www.amazon.com You can also redeem your gift card at checkout using this claim code: NE7N-TUD6NV-GUAB We hope to see you again soon, Amazon.com Once applied to your Amazon account, the entire amount will be added to you= r gift card balance. Your gift card balance can't be transferred to other a= ccounts, used to buy other gift cards, or, except as required by law, redee= med for cash. Your gift card balance will be applied automatically to eligible orders dur= ing the checkout process and when using 1-Click. If you don=E2=80=99t want = to use your gift card balance on your order, you can unselect it as a payme= nt method in checkout.=20 If you experience any issues using your gift card, you can reference your g= ift card by providing the following information to Customer Service: Order Number: 234343433433";
preg_match('/(?<=\bclaim\scode\b:\s)\S+/', $message, $matches);
echo $matches[0]; // NE7N-TUD6NV-GUAB
正面觀察:
確保給定的模式匹配,在表達式中的當前位置結束。圖案必須具有固定的寬度。不消耗任何字符。
語法:
(?<=...)
- 2 回答
- 0 關注
- 239 瀏覽
添加回答
舉報