Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regular expression question

Reply
Thread Tools

Regular expression question

 
 
George Moschovitis
Guest
Posts: n/a
 
      07-04-2004
Hello everyone!

I have a question regarding regular expressions. Let me explain
with an example. I have the string:

"{b}Text{/b} kokoko {b}Lalal{/b}"

and i want to convert it to this string

"<b>Text</b> kokoko <b>Lalal</b>"

if I use the following gsub:

str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')

I get:

"<b>Text{/b} kokoko {b}Lalal</b>"

not what I want. If I use

str.gsub!(/\{b\}([^\{]*)\{\/b\}/, '<b>\1</b>')

I get the correct anwser, but i dont like this
regular expression. Does ruby support an equivalent
to the /g option that perl provides? If not is there
a workaround? I would like to use something like:

str.gsub!(/\{b\}(.*)\{\/b\}/g, '<b>\1</b>')

Any help will be apreciated!

George Moschovitis

--
http://www.joy.gr
 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      07-04-2004
Hi --

On Sun, 4 Jul 2004, George Moschovitis wrote:

> Hello everyone!
>
> I have a question regarding regular expressions. Let me explain
> with an example. I have the string:
>
> "{b}Text{/b} kokoko {b}Lalal{/b}"
>
> and i want to convert it to this string
>
> "<b>Text</b> kokoko <b>Lalal</b>"
>
> if I use the following gsub:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')
>
> I get:
>
> "<b>Text{/b} kokoko {b}Lalal</b>"
>
> not what I want. If I use
>
> str.gsub!(/\{b\}([^\{]*)\{\/b\}/, '<b>\1</b>')
>
> I get the correct anwser, but i dont like this
> regular expression. Does ruby support an equivalent
> to the /g option that perl provides? If not is there
> a workaround? I would like to use something like:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/g, '<b>\1</b>')
>
> Any help will be apreciated!


Actually gsub *is* the /g equivalent The problem is that you need
to make the .* match non-greedy:

str.gsub!(/\{b}(.*?)\{\/b}/, '<b>\1</b>')

(You'd have to do this with /g in Perl too.)


David

--
David A. Black
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
 
 
 
Mark Sparshatt
Guest
Posts: n/a
 
      07-04-2004
George Moschovitis wrote:

>Hello everyone!
>
>I have a question regarding regular expressions. Let me explain
>with an example. I have the string:
>
>"{b}Text{/b} kokoko {b}Lalal{/b}"
>
>and i want to convert it to this string
>
>"<b>Text</b> kokoko <b>Lalal</b>"
>
>if I use the following gsub:
>
>str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')
>
>

try this

str.gsub!(/\{b\}(.*?)\{\/b\}/, '<b>\1</b>')


at the moment the .* is being too greedy when it's matching, causing it
to match
Text{/b} kokoko {b}Lalal
Using .*? switches off the greedy matching so it only matches up to the
first {/b}

HTH

--
Mark Sparshatt



 
Reply With Quote
 
Shajith CT
Guest
Posts: n/a
 
      07-04-2004
Hiya!

Use this:
str.gsub!(/\{b\}(.*?)\{\/b\}/, '<b>\1</b>')

Note the '?' in the regular expression: to make the match non-greedy.
Which in English means that it will match till the first "{/b}", and
not till the last possible one, which is the default case for some
reason. Which is why your regex was matching the longest possible
match of <b>...</b> in the string.

See http://www.regular-expressions.info/repeat.html
(Parent site's a pretty good regex reference)

-CT

On Sun, 4 Jul 2004 21:22:43 +0900, George Moschovitis <(E-Mail Removed)> wrote:
> Hello everyone!
>
> I have a question regarding regular expressions. Let me explain
> with an example. I have the string:
>
> "{b}Text{/b} kokoko {b}Lalal{/b}"
>
> and i want to convert it to this string
>
> "<b>Text</b> kokoko <b>Lalal</b>"
>
> if I use the following gsub:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')
>
> I get:
>
> "<b>Text{/b} kokoko {b}Lalal</b>"
>
> not what I want. If I use
>
> str.gsub!(/\{b\}([^\{]*)\{\/b\}/, '<b>\1</b>')
>
> I get the correct anwser, but i dont like this
> regular expression. Does ruby support an equivalent
> to the /g option that perl provides? If not is there
> a workaround? I would like to use something like:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/g, '<b>\1</b>')
>
> Any help will be apreciated!
>
> George Moschovitis
>
> --
> http://www.joy.gr
>
>



 
Reply With Quote
 
John Johnson
Guest
Posts: n/a
 
      07-04-2004
I realize you've gotten what is probably the most correct answer,
but here are a couple of alternatives:

str.gsub(/\{/, '<');
str.gsub(/\}/, '>');

or better still

str.gsub(/\{(.*?)\}/, '<\1>');

Which has the advantage of being a more general case, i.e. it will
work on {h1}, {center} or anything else in braces.

Regards,
JJ

On Sunday, July 4, 2004, at 08:22 AM, George Moschovitis wrote:

> Hello everyone!
>
> I have a question regarding regular expressions. Let me explain
> with an example. I have the string:
>
> "{b}Text{/b} kokoko {b}Lalal{/b}"
>
> and i want to convert it to this string
>
> "<b>Text</b> kokoko <b>Lalal</b>"
>
> if I use the following gsub:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/, '<b>\1</b>')
>
> I get:
>
> "<b>Text{/b} kokoko {b}Lalal</b>"
>
> not what I want. If I use
>
> str.gsub!(/\{b\}([^\{]*)\{\/b\}/, '<b>\1</b>')
>
> I get the correct anwser, but i dont like this
> regular expression. Does ruby support an equivalent
> to the /g option that perl provides? If not is there
> a workaround? I would like to use something like:
>
> str.gsub!(/\{b\}(.*)\{\/b\}/g, '<b>\1</b>')
>
> Any help will be apreciated!
>
> George Moschovitis
>
> --
> http://www.joy.gr
>




 
Reply With Quote
 
George Moschovitis
Guest
Posts: n/a
 
      07-05-2004
Thank you all for the answer!

George Moschovitis
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments