I have string {tender id="2,3"} or {tender catid="2" id="2,3"} or {tender catid="2"}
I use
$regex = "/{tender ((.*?id\s*=\s*['\"](.*?)['\"]) (.*?id\s*=\s*['\"](.*?)['\"]))}/";
but work only if i have in string both id and catid....How can i write regex to work if only one parameter is in string?
Try:
{tender (?:cat)?id="(\d+,?)+"}You may need to specify additional details if it doesn't work for you. As you requested, it will only match when either
catidor justidis in the data structure (case 1 and 3 for your examples).Explanation
{tenderthe start of the data.(?:cat)?catchcatif it canid="gets the start of an id(\d+,?)+gets digits that may or may not be followed by a comma"}End of the data.