Page 1 of 1

regex to preserve comments removing everything else

Posted: 28 Nov 2012 05:16
by neutrox
how do i extract info contained on

{...}
[...]
(...)
/*...*/
and <...>

using regex?

i mean: everything that's not between and of course including brackets must go.

Re: regex to preserve comments removing everything else

Posted: 28 Nov 2012 09:34
by highend
Probably not the most elegant way...

Code: Select all

$a = <<<>>>
This [text1] is...
This (text2) is...
This {text3} is...
This <text4> is...
This /*text5*/ is...
>>>;

	$b = regexreplace($a, ".*([[<({]|/\*)(.*?)([]>)}]|\*/).*", "$2");
	text $b;

Re: regex to preserve comments removing everything else

Posted: 09 Dec 2012 16:04
by neutrox
thanks much highend.

i've tried hard but can't let it work: is there a way to preserve all respective brackets?

Re: regex to preserve comments removing everything else

Posted: 09 Dec 2012 16:08
by highend
i've tried hard
And what did you try exactly?
is there a way to preserve all respective brackets?
Elaborate more...

Re: regex to preserve comments removing everything else

Posted: 09 Dec 2012 16:32
by neutrox
highend wrote:
i've tried hard
And what did you try exactly?
is there a way to preserve all respective brackets?
Elaborate more...
the attempts we made messing up the regex.

i need to have

[text1]
(text2)
{text3}
<text4>

instead of

text1
text2
text3
text4

Re: regex to preserve comments removing everything else

Posted: 09 Dec 2012 16:45
by highend
The regex itself doesn't need to be modified to accomplish that...

You just have to change the replacement from $2 to $1$2$3...

Re: regex to preserve comments removing everything else

Posted: 10 Dec 2012 19:07
by neutrox
highend wrote:The regex itself doesn't need to be modified to accomplish that...

You just have to change the replacement from $2 to $1$2$3...
solved, thanks much.