I describe the task completely, so that the context is clear: Get a specific page through curl. For example, this is a product page. The man clicked on the price. Determine the item. If it contains an id, stop. If not, look for the nearest parent with id. Then we look for the nearest parent with class. The element itself is already known to us.
I myself am not interested in finding items. It is interesting whether it is possible by the automaton to compose a regular expression that will find and get a number, having a sequence of parent id, nested class name and nested element. If possible, tell me how to do it correctly.