I have a string that looks like this:
‘p10005c4’
I need to extract the productId 10005 and the colorId 4 from that string into these variables:
$productId
$colorId
productId is > 10000.
colorId > 1.
How can I do this clean and nice using regex?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should be possible with the following regex:
This basically means, match any string that consists of a p, followed by one or more digits, then followed by a c, then followed by one or more digits.
The parentheses indicate capture groups, and since you want to capture the two ids, they’re surrounded by them.
To use this for your purposes, you’d do something like the following:
For more information on getting started with regular expressions, you might want to take a look at Regular-Expressions.info.