ruby - gsub same pattern from a string -


i have big problems figuring out how regex works. want text:

this example\e[213] text\e[123] demonstration

to become this:

this example text demonstration. 

so means want remove strings begin \e[ , end ] cant find proper regex this. current regex looks this:

/.*?(\\e\[.*\])?.*/ig 

but dont work. appreciate every help.

you need this:

txt.gsub(/\\e\[[^\]]*\]/i, "") 

there no need match before or after .*

the second problem use .* describe content between brackets. since * quantifier default greedy, match until last closing bracket in same line.

to prevent behaviour way use negated character class in place of dot excludes closing square brackets [^\]]. in way keep advantage of using greedy quantifier.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -