Regular expression: anything except

G

Gunther Gunt

Hello,

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

my text is:
<TEXT id=''>blabla</TEXT>
I want to replace <TEXT*> with something

"<TEXT id=''>".gsub(/the magic regular expression/,"replacement")
==> replacementblabla</TEXT>

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

and
<TEXT *anything*>blabla</TEXT>".gsub(/the magic regular
expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

Can you help me ?

Thank you.

Gunther
 
H

Hassan Schroeder

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

my text is:
<TEXT id=''>blabla</TEXT>
I want to replace <TEXT*> with something

"<TEXT id=''>".gsub(/the magic regular expression/,"replacement")
==> replacementblabla</TEXT>

This example isn't consistent with the following ones -- do you
but i want also that
"<TEXT>blabla</TEXT>".gsub(/the magic regular expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

and
<TEXT *anything*>blabla</TEXT>".gsub(/the magic regular
expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

Assuming the second two are actually what you want, try

"<TEXT id='foo'>blabla</TEXT>".gsub(/<TEXT[^>]*>/, 'replacement')

HTH,
 
L

Lars Haugseth

* Gunther Gunt said:
Hello,

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

my text is:
<TEXT id=''>blabla</TEXT>
I want to replace <TEXT*> with something

"<TEXT id=''>".gsub(/the magic regular expression/,"replacement")
==> replacementblabla</TEXT>

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

and
<TEXT *anything*>blabla</TEXT>".gsub(/the magic regular
expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

/<TEXT[^>]*>/
 
G

Gunther Gunt

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

my text is:
<TEXT id=''>blabla</TEXT>
I want to replace <TEXT*> with something

"<TEXT id=''>".gsub(/the magic regular expression/,"replacement")
==> replacementblabla</TEXT>

This example isn't consistent with the following ones -- do you
but i want also that
"<TEXT>blabla</TEXT>".gsub(/the magic regular expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

and
<TEXT *anything*>blabla</TEXT>".gsub(/the magic regular
expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

Assuming the second two are actually what you want, try

"<TEXT id='foo'>blabla</TEXT>".gsub(/<TEXT[^>]*>/, 'replacement')

HTH,

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

Thank you anyway ;o)

Gunther
 
M

Mark Thomas

Hello,

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

my text is:
<TEXT id=''>blabla</TEXT>
I want to replace <TEXT*> with something

"<TEXT id=''>".gsub(/the magic regular expression/,"replacement")
==> replacementblabla</TEXT>

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

and
<TEXT *anything*>blabla</TEXT>".gsub(/the magic regular
expression/,"replacement")
returns the same result
==> replacementblabla</TEXT>

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('<TEXT id="foo" anything="whatever">blabla</TEXT>')
puts xml.search("//TEXT").text

# => "blabla"

- Mark.
 
M

Mark Thomas

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</TEXT>

Oops! Now the regex doesn't work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top