Re: Regular Expressions for Clean URLS

Here is another interesting challenge.

I have a series of FAQs whose titles end in question marks. By default, my regex is converting them to dashes. How can I write a regex to lop off the last dash in a permalink (if it exists)?

Re: Regular Expressions for Clean URLS

Try this

permalink.sub(/-$/, '')

Railscasts - Free Ruby on Rails Screencasts

Re: Regular Expressions for Clean URLS

Yup that did it smile

For the record, what is the official difference between sub and gsub?

I have also seen sub! and gsub!

wassup with that?

Re: Regular Expressions for Clean URLS

Sub just replaces one occurance, gsub replaces multiple. For example:

"aaaa".sub('a', 'b') # => "baaa"
"aaaa".gsub('a', 'b') # => "bbbb"

The exclemation mark at the end makes the change in place, so if you have the string in a variable it will actually change that variable:

v = "test"
v.sub!('e', 'oa')
v # => "toast"

Railscasts - Free Ruby on Rails Screencasts

Re: Regular Expressions for Clean URLS

Dude, you know way too much of this stuff. I bet your dreams look like this

class Ryan < ActiveRecord::Base
  if self.unconscious
    render :action => 'ZZZ'
  end
end

Re: Regular Expressions for Clean URLS

Not quite... but getting there. wink

Railscasts - Free Ruby on Rails Screencasts

Re: Regular Expressions for Clean URLS

BTW, for the good folks following this thread, I wanted to share some great resources I found for regex.

http://www.txt2re.com/
This site is sort of like regex with training wheels. Youcan actually click on the letters or words you want to replace and it will spit out the necessary expressions.

http://www.fileformat.info/tool/regex.htm
You can test your regex here...much quicker than poking with script/console

http://regexlib.com/Default.aspx
Chances are, someone has already made the regex you are looking for. WHy reinvent the wheel? Search over 1,000 expressions on this site!

Hope yall find this as useful as I did smile

Re: Regular Expressions for Clean URLS

Hi, guys!

I've just read this thred and I've got a similar question. I'm using an application helper what is use for cleaning up the HTML strings. This function replaces the \n parts to valid <br /> tags and so on...


I wanted to convert the URLs into hyperlinks, so I just inserted this into the function:

def cleanup(s)
  s.gsub!(/<[^>]*>/, "")
  s.gsub!("\n", '<br/>')
  s.gsub!("/^(http|https):\/\/\S+/", "<a href='\0'>link</a>
end

The idea is the following:

- In the first row i replace every HTML tag (works fine)
. Nex step is to convert \n-s into <br /> tags.

For here, everything works fine. Only the last command is a "little bit wrong" - Actually it crashes my whole rails application.

The idea is to use RegEXP to find every string what starts with 'http://' or 'https://' and convert the whole string into a hyperlink, by inserting the anchor tags before and after them.

As I've wrote, the last row destroys my whole rails app. Can anyone help pls? Is the RegEXP wrong or what? (I was trying to find a similar workaround for URLs - there must be at least one... - but I haven't find anything about that. As a last hop, I dropped it here as a question for you expert guys, you'll probably know a workaround for this!

Thanks a lot
Liquid

Last edited by liquid (2007-05-25 15:49:35)

Re: Regular Expressions for Clean URLS

Rails has the auto_link helper method which basically does this.

Railscasts - Free Ruby on Rails Screencasts

Re: Regular Expressions for Clean URLS

i just love this forum! u are the man, ryan! wink

thnx a lot, i've learned so much from here! keep up this good community work!

Re: Regular Expressions for Clean URLS

Got another small challenge.

I am using a custom method for auto-excerpts and would like to exclude any textile images. Basically I need to ignore anything that looks like this

!/path/to/image.gif-or-jpg!

I don't want to delete this content, just have my view ignore it.

Any ideas?

Re: Regular Expressions for Clean URLS

This thread is getting long, and I don't see how it's related so you might want to create a new thread on this topic and go into more detail.

Railscasts - Free Ruby on Rails Screencasts