[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

using a variable to build a regex

Dave Thacker

12/23/2007 3:19:00 AM

I need to search a roster file (plain text) to match for a player name. I
tried this:

dthacker@buckbeak:~/learning/ruby$ irb
irb(main):001:0> player="S_Avrul"
=> "S_Avrul"
irb(main):002:0> player_line="S_Avrul ger 25 R 1 2 3 7 40 35"
=> "S_Avrul ger 25 R 1 2 3 7 40 35"
irb(main):003:0> re=/player/
=> /player/
irb(main):004:0> re.match(player_line)
=> nil
irb(main):005:0>

How can I use the value of "player" to create the regex?

TIA Dave




3 Answers

Tim Hunter

12/23/2007 3:24:00 AM

0

Dave Thacker wrote:
> I need to search a roster file (plain text) to match for a player name. I
> tried this:
>
> dthacker@buckbeak:~/learning/ruby$ irb
> irb(main):001:0> player="S_Avrul"
> => "S_Avrul"
> irb(main):002:0> player_line="S_Avrul ger 25 R 1 2 3 7 40 35"
> => "S_Avrul ger 25 R 1 2 3 7 40 35"
> irb(main):003:0> re=/player/
> => /player/
> irb(main):004:0> re.match(player_line)
> => nil
> irb(main):005:0>
>
> How can I use the value of "player" to create the regex?
>
> TIA Dave
>
>
>
>

/#{player}/

--
RMagick: http://rmagick.ruby...

Phrogz

12/23/2007 3:28:00 AM

0

On Dec 22, 8:18 pm, Dave Thacker <dthac...@bluestrain.net> wrote:
> dthacker@buckbeak:~/learning/ruby$ irb
> irb(main):001:0> player="S_Avrul"
> => "S_Avrul"
> irb(main):002:0> player_line="S_Avrul ger 25 R 1 2 3 7 40 35"
> => "S_Avrul ger 25 R 1 2 3 7 40 35"
> irb(main):003:0> re=/player/
> => /player/
> irb(main):004:0> re.match(player_line)
> => nil
> irb(main):005:0>            
>
> How can I use the value of "player" to create the regex?

In a string literal, "player" would also be the actual string "player"
and not the value of that variable. Do you know how to substitute
inside a string? The same answer applies to Regexp literals in Ruby.

For a hint:
http://phrogz.net/ProgrammingRuby/language.html#sub...

(In

Ken Bloom

12/23/2007 4:29:00 AM

0

Dave Thacker <dthacker@bluestrain.net> wrote:
> I need to search a roster file (plain text) to match for a player name. I
> tried this:
>
> dthacker@buckbeak:~/learning/ruby$ irb
> irb(main):001:0> player="S_Avrul"
> => "S_Avrul"
> irb(main):002:0> player_line="S_Avrul ger 25 R 1 2 3 7 40 35"
> => "S_Avrul ger 25 R 1 2 3 7 40 35"
> irb(main):003:0> re=/player/
> => /player/
> irb(main):004:0> re.match(player_line)
> => nil
> irb(main):005:0>
>
> How can I use the value of "player" to create the regex?

You can use #{} substitution in a Regexp, the same way you can in a
string. Thus re=/#{player}/. You can also use Regexp.new to construct
a regexp from a string. (So you can do ordinary string manipulations
to construct the regexp as a string, then turn that into a regexp.)

Both of these will treat any metacharacters in the string as
metacharacters (e.g. A "." in the string will match any character). To
avoid this problem, use Regexp.escape.

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...