Forum: Ruby on Rails regular expressions slay me

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 04:50
(Received via mailing list)
I am furiously trying to find what I am looking for in Pickaxe book and
not finding it.

I'm getting some values back from a posted form and I need to get the
'id' number off the backend. The 'String' will always come back to me
as...

Some_name\r\n123

where 123 is actually the id number which I need to execute the find.
The reason they are coming back this way is from a composite
'text_auto_complete_for' methodology which is allowing users to pick by
name and I want the associated id column for an easy find.

But I can't get to square one with irb...

irb(main):021:0> myvar = "Some_name\r\n123"
=> "Some_name\r\n123"
irb(main):022:0> show_regexp(myvar, /\w+/)
NoMethodError: undefined method `show_regexp' for main:Object
        from (irb):22
        from :0

Please someone...

a. tell me what type of command I would need to pick the numbers off the
end

b. tell me why irb doesn't do something so obviously in the Pickaxe
book.

Thanks

Craig
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-03-09 04:56
(Received via mailing list)
Craig White wrote:
> I am furiously trying to find what I am looking for in Pickaxe book and
> not finding it.
>
> I'm getting some values back from a posted form and I need to get the
> 'id' number off the backend. The 'String' will always come back to me
> as...
>
> Some_name\r\n123

So you actually have a multi-line string being returned...
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 04:59
(Received via mailing list)
On Wed, 2006-03-08 at 22:53 -0500, Brian V. Hughes wrote:
> So you actually have a multi-line string being returned...
> > irb(main):022:0> show_regexp(myvar, /\w+/)
> > book.
----
apparently, a multi-line string is being returned...

Yes

Craig
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-09 05:02
(Received via mailing list)
On Mar 8, 2006, at 7:48 PM, Craig White wrote:

> where 123 is actually the id number which I need to execute the find.
> NoMethodError: undefined method `show_regexp' for main:Object
>         from (irb):22
>         from :0
>
> Please someone...
>
> a. tell me what type of command I would need to pick the numbers
> off the
> end

/(\d+)$/

Answer in $1

> b. tell me why irb doesn't do something so obviously in the Pickaxe
> book.

Page 93 of the Pick Axe shows give this for show_regex...

def show_regexp(a, re)
   if a =~ re
     "#{$`}<<#{$&}>>#{$'}"
   else
     "no match"
   end
end

Have you defined that method before using it?

--
-- Tom Mornini
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-03-09 05:02
(Received via mailing list)
Ignore that last message. Stupid email client! :)

Craig White wrote:
> I am furiously trying to find what I am looking for in Pickaxe book and
> not finding it.
>
> I'm getting some values back from a posted form and I need to get the
> 'id' number off the backend. The 'String' will always come back to me
> as...
>
> Some_name\r\n123

Right... where was I? Oh, yeah. The returned value is a multi-line
field. But that's not going to be a problem in this case because getting
the second line is really quite simple.

> NoMethodError: undefined method `show_regexp' for main:Object
>         from (irb):22
>         from :0

You can't use show_regexp like that unless you've recently defined it as
a stand alone method. That's why irb is telling you it's undefined.

> Please someone...
>
> a. tell me what type of command I would need to pick the numbers off the
> end

Give this a try: id_num = "Some_name\r\n123".split("\r\n")[1]

> b. tell me why irb doesn't do something so obviously in the Pickaxe
> book.

I don't really have an answer on that, but from a Ruby standpoint, I'd
say you were making an incorrect attempt at calling a method. I've never
looked at show_regexp, but I'd guess it's a String or Regex object
method, so you would need to treat it as such, if you want to have it
return a usable value.

-Brian
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian V. Hughes (Guest)
on 2006-03-09 05:05
(Received via mailing list)
Brian V. Hughes wrote:
> Craig White wrote:
>>
>> Some_name\r\n123
>>
>> a. tell me what type of command I would need to pick the numbers off the
>> end
>
> Give this a try: id_num = "Some_name\r\n123".split("\r\n")[1]

I'd make one quick ammendment to my statement above:

     id_num = "Some_name\r\n123".split("\r\n")[1].to_i

That way, you know you're getting back a Fixnum value, not a string.

-Brian
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-03-09 05:05
(Received via mailing list)
Hi Craig, here is an alternative to doing something like that, this
relies
on this patch which I hope gets included:

Patch: http://dev.rubyonrails.org/ticket/3691


In the view, your typical auto_complete, note the hidden field for the
id,
and the javascript callback (after_update_element):

<%= text_field_tag 'client', '' %>
<%= hidden_field 'encounter', 'client_id' %>
<div id="client_auto_complete" class="auto_complete"></div>
<%= auto_complete_field 'client', :url => {:controller => 'clients',
:action
=> 'auto_complete_for_client'}, :after_update_element =>
'auto_complete_on_select', :select => 'name' %>


Also in your view, in a script block. Here I use the DOM to grab the ID:

function auto_complete_on_select(element, selectedElement)
{
  document.getElementById('encounter_client_id').value =
selectedElement.childNodes.item(0).innerHTML;
}


And finally, the partial for the autocomplete:

<ul class="clients">
<% for client in @clients do -%>
  <li class="client">
    <span class="informal"><%= client.id -%></span>
    <div class="name"><%= h client.name %></div>
  </li>
<% end -%>
</ul>

The informal hides the ID element from view and the javascript uses that
to
populate the hidden field, so that your ID is available when you submit
the
form.

To answer your original question:

irb(main):019:0> var = "Some_name\r\n123"
=> "Some_name\r\n123"
irb(main):020:0> var =~ /([[:digit:]+])/m
=> 11

For the show_regexp, do you have the function defined? Its not a ruby
function.

def show_regexp(a, re)
  if a =~ re
    "#{$`}<<#{$&}>>#{$'}"
  else
    "no match"
  end
end


Bob Silva
http://www.railtie.net/
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-09 05:05
(Received via mailing list)
Hi --

On Wed, 8 Mar 2006, Craig White wrote:

> The reason they are coming back this way is from a composite
>        from :0
>
> Please someone...
>
> a. tell me what type of command I would need to pick the numbers off the
> end

If those are definitely the only numbers in the string, you could do:

   numbers = myvar[/\d+/]

meaning: the substring of myvar that consists of one-or-more digits
(\d+).

If you need to specify the digits at the end of the entire string (as
opposed to any possible other digits), you would use an "anchor" in
the regular expression, to indicate end-of-string:

   numbers = myvar[/\d+\Z/]

The \Z anchor actually means: end of string, give or take a trailing
\n.  \z means absolute end of string.  \Z is preferable if you're not
sure whether there's a trailing \n and don't want to worry about it.

> b. tell me why irb doesn't do something so obviously in the Pickaxe
> book.

I can't put my finger on show_regexp in the Pickaxe but I think the
answer is that it's defined there.  It's not part of Ruby; that's why
irb doesn't know about it :-)


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 05:08
(Received via mailing list)
On Wed, 2006-03-08 at 19:59 -0800, Tom Mornini wrote:
> > Some_name\r\n123
> > => "Some_name\r\n123"
>
>    if a =~ re
>      "#{$`}<<#{$&}>>#{$'}"
>    else
>      "no match"
>    end
> end
>
> Have you defined that method before using it?
----
No - I'm looking at page 73 and I'm seeing the examples and thinking,
heck, they work there...

I hadn't gotten to p. 93 yet

;-)

Thanks...between the two answers, I can probably make it work but you
can understand my desire to use irb to test these things out so I don't
have to bother the list.

Craig
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 05:29
(Received via mailing list)
I'm gonna have to take some time to digest this...interesting that you
come around with this now that I have all of the puzzle solved.

This one was a several week puzzle. It starts with aggregation fields,
using the aggregation for auto_complete and finally, I was able to get
the id # along for the ride because finding on aggregation fields can be
a problem. I can do it the 'rails' way, but I've been spoiled by Ezra's
ez_where plug-in which tremendously simplify complex SQL finds. The only
limitation I have run into with Ezra's ez_where plug-in is including a
series of concatenated columns in a postgres table and now I can finally
end run around that limitation (and I say limitation but I doubt that
what I am doing is commonly done with SQL finds).

Anyway, thanks and I'm going to investigate it.

Craig
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 05:29
(Received via mailing list)
On Wed, 2006-03-08 at 23:02 -0500, Brian V. Hughes wrote:
> I'd make one quick ammendment to my statement above:
>
>      id_num = "Some_name\r\n123".split("\r\n")[1].to_i
>
> That way, you know you're getting back a Fixnum value, not a string.
----
the first one slayed my dragon...I got the concept though thanks

Craig
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-09 05:35
(Received via mailing list)
On Wed, 2006-03-08 at 20:04 -0800, dblack@wobblini.net wrote:
> >
> > => "Some_name\r\n123"
> If those are definitely the only numbers in the string, you could do:
>    numbers = myvar[/\d+\Z/]
> irb doesn't know about it :-)
----
thanks David - this actually gave me a clearer idea of the methodology.

I probably have to start digging in at the beginning of the chapter of
the pickaxe book to figure out why he's giving examples that won't work
in irb - and as you and others have pointed out, what I assumed to be a
ruby command...show_regexp is not a ruby command at all and would have
to be some kind of method that I hadn't created. I did read it once
linearly and try to use it as reference but of course not in a linear
fashion and this time it frustrated me.

I hope that you appreciate that I wanted to work through the regular
expressions and figured irb was the lab to do that but there are times
when I don't have what I need in irb at my disposal.

Craig
119af50160cabfe1fb6f2f05f5018c64?d=identicon&s=25 James Ludlow (Guest)
on 2006-03-10 06:27
(Received via mailing list)
On 3/8/06, Craig White <craigwhite@azapple.com> wrote:
> I hope that you appreciate that I wanted to work through the regular
> expressions and figured irb was the lab to do that but there are times
> when I don't have what I need in irb at my disposal.

On a related note, "Mastering Regular Expressions" by Jeffrey Friedl
is easily one of the best software-related books I've ever read.

http://www.oreilly.com/catalog/regex2/


-- James
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-10 07:01
(Received via mailing list)
On Thu, 2006-03-09 at 23:25 -0600, James Ludlow wrote:
> On 3/8/06, Craig White <craigwhite@azapple.com> wrote:
> > I hope that you appreciate that I wanted to work through the regular
> > expressions and figured irb was the lab to do that but there are times
> > when I don't have what I need in irb at my disposal.
>
> On a related note, "Mastering Regular Expressions" by Jeffrey Friedl
> is easily one of the best software-related books I've ever read.
>
> http://www.oreilly.com/catalog/regex2/
----
thanks, I'll file that in books that I should read but don't want to
read (like Dave's subversion book and many others) because I don't use
it enough to want to be expert at it...at most, it's a once in a great
while thing. My issue was getting the ability to test within irb and it
was pointed out to me that somewhere earlier in the chapter, I
undoubtedly missed a 'method' that made the samples throughout the
chapter work and I clearly wasn't working the book in a linear fashion.

Craig
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-03-10 14:57
(Received via mailing list)
Also check out one of the best interactive regexp gui tools...
http://www.weitz.de/regex-coach/
-bakki
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (Guest)
on 2006-03-10 15:40
(Received via mailing list)
You can also check out
- http://www.sellsbrothers.com/tools/#regexd (nice gui tools for
designing
regexp)
- http://www.regular-expressions.info/ (good site with tutorials)

hth

Thibaut
This topic is locked and can not be replied to.