The problem is quite easy. I would like to match anything between some strings at the beginning and some strings at the end. Strings at the end should match appropriate strings at the beginning.
Let's assume that I want to match everything that is between [
and ]
or {
and }
.
The first regular expression that could be used is:
/[{\[](.*)[}\]]/gmU
however there is one problem with it. When subject is:
{aa} werirweiu [ab] wrewre [ac}
also [ac}
is matched but it shouldn't.
It can be easily changed into:
/\[(.*)\]|\{(.*)\}/gmU
and the problem is solved.
But what in case if (.*)
were much more complicated and beginnings and ends would be for example 10 and they also would be a bit more complicated (not one character but many)? Then using above rule the whole (.*)
should be repeated 10 times and it would be illegible.
Is there any way to match ends with beginnings? For example I would like to use syntax similar to
/(aa|bb)(.*)(cc|ddd)/gmU
to tell that match must begin with aa
and ends with cc
or begin with bb
and ends with ddd
and match in subject aaxx1cc bbxx2ddd aaxx3ddd bbxx4cc
only strings xx1
and xx2
without repeating (.*)
many times in that regular expression and remembering there might be more than 2 as in above examples beginnings and endings.