regex to preserve comments removing everything else

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

regex to preserve comments removing everything else

Post 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.

highend
Posts: 14639
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: regex to preserve comments removing everything else

Post 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;
One of my scripts helped you out? Please donate via Paypal

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: regex to preserve comments removing everything else

Post by neutrox »

thanks much highend.

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

highend
Posts: 14639
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: regex to preserve comments removing everything else

Post by highend »

i've tried hard
And what did you try exactly?
is there a way to preserve all respective brackets?
Elaborate more...
One of my scripts helped you out? Please donate via Paypal

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: regex to preserve comments removing everything else

Post 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

highend
Posts: 14639
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: regex to preserve comments removing everything else

Post 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...
One of my scripts helped you out? Please donate via Paypal

neutrox
Posts: 194
Joined: 05 Mar 2012 15:23

Re: regex to preserve comments removing everything else

Post 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.

Post Reply