# Regular expression: anything except

**URL:** <https://www.ruby-forum.com/t/regular-expression-anything-except/145745>\
**Category:** Ruby\
**Created:** [August 13, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745 "2008-08-13T16:12:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/1 "2008-08-13T16:12:58Z")

</div>

Hello,

I am trying to create a regular expression with the following rule:

my text is:  
blabla  
I want to replace \<TEXT\*\> with something

“”.gsub(/the magic regular expression/,“replacement”)  
==\> replacementblabla

but i want also that  
“blabla”.gsub(/the magic regular expression/,“replacement”)  
returns the same result  
==\> replacementblabla

and  
\<TEXT _anything_\>blabla".gsub(/the magic regular  
expression/,“replacement”)  
returns the same result  
==\> replacementblabla

Can you help me ?

Thank you.

Gunther

---

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/2 "2008-08-13T16:26:24Z")

</div>

On Wed, Aug 13, 2008 at 7:10 AM, Gunther G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to create a regular expression with the following rule:
> 
> my text is:  
> blabla  
> I want to replace \<TEXT\*\> with something
> 
> “”.gsub(/the magic regular expression/,“replacement”)  
> ==\> replacementblabla

This example isn’t consistent with the following ones – do you  
want the ending inserted even if it’s not in the original  
string?

> but i want also that  
> “blabla”.gsub(/the magic regular expression/,“replacement”)  
> returns the same result  
> ==\> replacementblabla
> 
> and  
> \<TEXT _anything_\>blabla".gsub(/the magic regular  
> expression/,“replacement”)  
> returns the same result  
> ==\> replacementblabla

Assuming the second two are actually what you want, try

“blabla”.gsub(/\<TEXT[^\>]\*\>/, ‘replacement’)

HTH,

---

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/3 "2008-08-13T16:37:15Z")

</div>

Hassan S. wrote:

> On Wed, Aug 13, 2008 at 7:10 AM, Gunther G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am trying to create a regular expression with the following rule:
> > 
> > my text is:  
> > blabla  
> > I want to replace \<TEXT\*\> with something
> > 
> > “”.gsub(/the magic regular expression/,“replacement”)  
> > ==\> replacementblabla
> 
> This example isn’t consistent with the following ones – do you  
> want the ending inserted even if it’s not in the original  
> string?
> 
> > but i want also that  
> > “blabla”.gsub(/the magic regular expression/,“replacement”)  
> > returns the same result  
> > ==\> replacementblabla
> > 
> > and  
> > \<TEXT _anything_\>blabla".gsub(/the magic regular  
> > expression/,“replacement”)  
> > returns the same result  
> > ==\> replacementblabla
> 
> Assuming the second two are actually what you want, try
> 
> “blabla”.gsub(/\<TEXT[^\>]\*\>/, ‘replacement’)
> 
> HTH,

I had just found the solution, and I was posting it.

Thank you anyway ;o)

Gunther

---

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 4:50pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/4 "2008-08-13T16:50:28Z")

</div>

- Gunther G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ==\> replacementblabla  
> ==\> replacementblabla  
> /\<TEXT[^\>]\*\>/

–  
Lars H.

“If anyone disagrees with anything I say, I am quite prepared not only  
to  
retract it, but also to deny under oath that I ever said it.” -Tom  
Lehrer

---

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/5 "2008-08-13T19:35:54Z")

</div>

I said:

> Since it looks like XML, I recommend using an XML parser. It not  
> a good idea to parse XML with regular expressions; doing it robustly  
> is difficult, as there are many details that can trip you up.

\< TEXT id=“foo”\>blabla

Oops! Now the regex doesn’t work.

---

<div class="post-metadata">

**Author:** ![gunbol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/13edae/32.png) [@gunbol](https://www.ruby-forum.com/u/gunbol)\
**Post date:** [August 13, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/regular-expression-anything-except/145745/6 "2008-08-13T16:41:05Z")

</div>

On Aug 13, 10:10 am, Gunther G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you help me ?

Sure. Since it looks like XML, I recommend using an XML parser. It not  
a good idea to parse XML with regular expressions; doing it robustly  
is difficult, as there are many details that can trip you up.

What you want is the text content of the TEXT element. Try this:

require ‘hpricot’  
xml = Hpricot::XML(‘blabla’)  
puts xml.search(“//TEXT”).text

# =\> “blabla”

- Mark.
