[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

short regexp question

Fritzek

9/18/2008 2:53:00 PM

Hi folks

short question how to use regexp the right way
given is a="a[b]c"
I nedd b="a c"

tried to split using (/(\[|\])/) to get ["a", "[", "b", "]", "c"]

b=a.strip.split(/(\[|\])/)

and then joined the bits together. this just works in a simple case
like "a[b]c", but "[b]" could occur multiple times.

I need something like search for any "[b]" and substitute with a
blank.

Thanks in advance

Fritzek
18 Answers

David A. Black

9/18/2008 2:54:00 PM

0

Hi --

On Thu, 18 Sep 2008, Fritzek wrote:

> Hi folks
>
> short question how to use regexp the right way
> given is a="a[b]c"
> I nedd b="a c"
>
> tried to split using (/(\[|\])/) to get ["a", "[", "b", "]", "c"]
>
> b=a.strip.split(/(\[|\])/)
>
> and then joined the bits together. this just works in a simple case
> like "a[b]c", but "[b]" could occur multiple times.
>
> I need something like search for any "[b]" and substitute with a
> blank.

b = a.delete("[b]")


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

Fritzek

9/18/2008 3:06:00 PM

0

Hi David

thanks for quick answer. your code just works, if you know "b". I only
know the surrounding brackets "[" and "]" The bit in between could be
everything. sorry, forgot to mention.

Fritzek

On 18 Sep., 16:54, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
>
>
> On Thu, 18 Sep 2008, Fritzek wrote:
> > Hi folks
>
> > short question how to use regexp the right way
> > given is a="a[b]c"
> > I nedd b="a c"
>
> > tried to split using (/(\[|\])/) to get  ["a", "[", "b", "]", "c"]
>
> > b=a.strip.split(/(\[|\])/)
>
> > and then joined the bits together. this just works in a simple case
> > like "a[b]c", but "[b]" could occur multiple times.
>
> > I need something like search for any "[b]" and substitute with a
> > blank.
>
> b = a.delete("[b]")
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
>    Intro to Ruby on Rails  January 12-15   Fort Lauderdale, FL
>    Advancing with Rails    January 19-22   Fort Lauderdale, FL *
>    * Co-taught with Patrick Ewing!
> Seehttp://www.ruby... details and updates!

Brian Candler

9/18/2008 3:07:00 PM

0

> I need something like search for any "[b]" and substitute with a
> blank.

b = a.gsub(/\[b\]/,' ')
--
Posted via http://www.ruby-....

Brian Candler

9/18/2008 3:09:00 PM

0

> b = a.gsub(/\[b\]/,' ')

Also possibly useful is for you:

a = "aaa[b]bbb[b]ccc"
bits = a.split(/\[b\]/)

--
Posted via http://www.ruby-....

Fritzek

9/18/2008 3:23:00 PM

0

Hi Brian

thanks for your answer. as I stated to David, I just know about the
surrounding brackets not the bits between them.

Fritzek

On 18 Sep., 17:08, Brian Candler <b.cand...@pobox.com> wrote:
> > b = a.gsub(/\[b\]/,' ')
>
> Also possibly useful is for you:
>
> a = "aaa[b]bbb[b]ccc"
> bits = a.split(/\[b\]/)
>
> --
> Posted viahttp://www.ruby-....

Sebastian Hungerecker

9/18/2008 3:32:00 PM

0

Fritzek wrote:
> short question how to use regexp the right way
> given is a="a[b]c"
> I nedd b="a c"

"a[b]c".gsub(/\[.*?\]/, " ")

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Fritzek

9/18/2008 3:48:00 PM

0

Hi Sebastian

thanks for the solution. works perfect.

Fritzek

On 18 Sep., 17:31, Sebastian Hungerecker <sep...@googlemail.com>
wrote:
> Fritzek wrote:
> > short question how to use regexp the right way
> > given is a="a[b]c"
> > I nedd b="a c"
>
> "a[b]c".gsub(/\[.*?\]/, " ")
>
> HTH,
> Sebastian
> --
> Jabber: sep...@jabber.org
> ICQ: 205544826

Robert Klemme

9/18/2008 8:14:00 PM

0

On 18.09.2008 17:47, Fritzek wrote:
> Hi Sebastian
>
> thanks for the solution. works perfect.
>
> Fritzek
>
> On 18 Sep., 17:31, Sebastian Hungerecker <sep...@googlemail.com>
> wrote:
>> Fritzek wrote:
>>> short question how to use regexp the right way
>>> given is a="a[b]c"
>>> I nedd b="a c"
>> "a[b]c".gsub(/\[.*?\]/, " ")

Not sure whether it makes a difference performance wise but I am always
reluctant to use reluctant quantifiers. I'd rather do

irb(main):003:0> "a[b]c".gsub /\[[^\]]*\]/, ' '
=> "a c"

Kind regards

robert

Fritzek

9/19/2008 1:14:00 PM

0

Hi Robert

thanks for your objection, but could you shortly explain the
difference (for regexp dummies like me)?

Fritzek

On 18 Sep., 22:13, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 18.09.2008 17:47, Fritzek wrote:
>
> > Hi Sebastian
>
> > thanks for the solution. works perfect.
>
> > Fritzek
>
> > On 18 Sep., 17:31, Sebastian Hungerecker <sep...@googlemail.com>
> > wrote:
> >> Fritzek wrote:
> >>> short question how to use regexp the right way
> >>> given is a="a[b]c"
> >>> I nedd b="a c"
> >> "a[b]c".gsub(/\[.*?\]/, " ")
>
> Not sure whether it makes a difference performance wise but I am always
> reluctant to use reluctant quantifiers.  I'd rather do
>
> irb(main):003:0> "a[b]c".gsub /\[[^\]]*\]/, ' '
> => "a c"
>
> Kind regards
>
>         robert

Robert Klemme

9/19/2008 1:23:00 PM

0

2008/9/19 Fritzek <fritz.thielemann@googlemail.com>:
> thanks for your objection, but could you shortly explain the
> difference (for regexp dummies like me)?

Ideally you read "Mastering Regular Expressions" which explains such
topics very nicely.

I believe it is generally better to be more specific about what is to
match (mainly for robustness reasons). Also, with the reluctant
quantifier for every character in the input a match against the next
sub pattern needs to be tested OR there needs to be backtracking to
find out whether there is a shorter match afterwards. Both seem not
very efficient. Granted, this is no hard evidence, but if you are
curious I suggest you do some benchmarks and read the book; it's
really good!

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end