regexp: pattern in pattern
Moderator: General Moderators
regexp: pattern in pattern
Hi,
I'd like to write a regexp which solves this kond of problem:
basic text=" aaa [[aaa bbb [[ccc ddd]] eee ]] bbb "
regxp=?
as a result I'd like to get : [[aaa bbb [[ccc ddd]] eee ]]
what I am able to get is only: [[aaa bbb [[ccc ddd]]
I'm using python, so I't would be great if it worked in python aswell.
thanks in advance
I'd like to write a regexp which solves this kond of problem:
basic text=" aaa [[aaa bbb [[ccc ddd]] eee ]] bbb "
regxp=?
as a result I'd like to get : [[aaa bbb [[ccc ddd]] eee ]]
what I am able to get is only: [[aaa bbb [[ccc ddd]]
I'm using python, so I't would be great if it worked in python aswell.
thanks in advance
Re: regexp: pattern in pattern
Code: Select all
$s = ' aaa [[aaa bbb [[ccc ddd]] eee ]] bbb ';
$s = preg_replace('/^[^[]*(\[.*\])[^\]]*$/','\\1',$s);
// $s is now what you want- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: regexp: pattern in pattern
Can there be more nested brackets? Like this:
If so, then no, there is no regex solution for this: regex is not meant to "count" or being able to create recursive patterns. At least, last I checked, Python is not able to do this. You'd better ask at a Python specific forum/mailing list to be really sure.
Good luck.
Code: Select all
... [[ ... [[ ... [[ ... ]] ... ]] ... ]] ...Good luck.
Re: regexp: pattern in pattern
My solution above works just fine on such stringsprometheuzz wrote:If so, then no, there is no regex solution for this
Unless I misunderstood TS's problem, I assumed the regexp was supposed to cut off anything until the first [ and beyond the last ], right?
Re: regexp: pattern in pattern
Backus naur form ftw!If so, then no, there is no regex solution for this
The problem looks like something you would have to solve using a recursive definition.
I don't recall how to do any of this stuff but I remember something about lexx and yacc from writing language parsers at university.
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: regexp: pattern in pattern
If only one such a string exists, the yes. But I presumed (and still do) that the OP over simplified his/her problem and that s/he can have string like these:Apollo wrote:My solution above works just fine on such stringsprometheuzz wrote:If so, then no, there is no regex solution for this
Code: Select all
'aaa [[bbb [[ ccc ]] ]] ddd [[ eee fff ]] ggg'Code: Select all
'[[bbb [[ ccc ]] ]]'Code: Select all
'[[ eee fff ]]'Re: regexp: pattern in pattern
Yes, unfortunatelly that kind of srtings are also possible.prometheuzz wrote:
If only one such a string exists, the yes. But I presumed (and still do) that the OP over simplified his/her problem and that s/he can have string like these:
whereCode: Select all
'aaa [[bbb [[ ccc ]] ]] ddd [[ eee fff ]] ggg'
andCode: Select all
'[[bbb [[ ccc ]] ]]'
are the sub strings the OP is interested in. And if there can be more than 2 nested tags then regex is definately not the way to go (especially not with Python).Code: Select all
'[[ eee fff ]]'
In string like:
Code: Select all
'aaa [[bbb [[ ccc ]] ]] ddd [[ eee fff ]] gggI need to find
Code: Select all
'[[bbb [[ ccc ]] ]]'Code: Select all
'[[ eee fff ]]'as you've written
Any solution?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: regexp: pattern in pattern
I believe I already answered that question (more than once) ; )funfeltp wrote:...
Any solution?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: regexp: pattern in pattern
That is correct (see my first reply as well).mintedjo wrote:Backus naur form ftw!If so, then no, there is no regex solution for this
The problem looks like something you would have to solve using a recursive definition.
PHP's regex engine can cope with recursively nested tags, but it is a pain in the @ss to get your head around the concept. Writing a little grammar and then generating a lexer+parser with tools as Lexx, Yacc, ANTLR, etc. would be "the way" to go.mintedjo wrote:I don't recall how to do any of this stuff but I remember something about lexx and yacc from writing language parsers at university.
Anything but regex! ; )
Re: regexp: pattern in pattern
OK I get it : )prometheuzz wrote:[
I believe I already answered that question (more than once) ; )
I see that I'm gonna have to do something else with that sh..t.
Thank's anyway.