[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regular expression: anything except

Gunther Gunt

8/13/2008 2:10:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Hassan Schroeder

8/13/2008 2:23:00 PM

0

On Wed, Aug 13, 2008 at 7:10 AM, Gunther Gunt
<gunther.thevenin@airbus.com> wrote:

> 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
want the ending </TEXT> inserted even if it's not in the original
string?

> 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,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

Lars Haugseth

8/13/2008 2:32:00 PM

0

* Gunther Gunt <gunther.thevenin@airbus.com> wrote:
>
> 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[^>]*>/

--
Lars Haugseth

"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

Gunther Gunt

8/13/2008 2:35:00 PM

0

Hassan Schroeder wrote:
> On Wed, Aug 13, 2008 at 7:10 AM, Gunther Gunt
> <gunther.thevenin@airbus.com> wrote:
>
>> 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
> want the ending </TEXT> inserted even if it's not in the original
> string?
>
>> 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
--
Posted via http://www.ruby-....

Mark Thomas

8/13/2008 2:39:00 PM

0

On Aug 13, 10:10 am, Gunther Gunt <gunther.theve...@airbus.com> wrote:
> 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.

Mark Thomas

8/13/2008 5:33:00 PM

0

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.