views:

85

answers:

3

Hi,

Could anybody help me make a proper regular expression from a bunch of text in Ruby. I tried a lot but I don't know how to handle variable length titles.

The string will be of format <sometext>title:"<actual_title>"<sometext>. I want to extract actual_title from this string.

I tried /title:"."/ but it doesnt find any matches as it expects a closing quotation after one variable from opening quotation. I couldn't figure how to make it check for variable length of string. Any help is appreciated. Thanks.

+2  A: 
/title:"([^"]*)"/

The parentheses create a capturing group. Inside is first a character class. The ^ means it's negated, so it matches any character that's not a ". The * means 0 or more. You can change it to one or more by using + instead of *.

Matthew Flaschen
Awesome. Working very well, although I didn't quite understand what you have done..
Sainath Mallidi
Thanks a lot for the explanation. :)
Sainath Mallidi
+2  A: 

. matches any single character. Putting + after a character will match one or more of those characters. So .+ will match one or more characters of any sort. Also, you should put a question mark after it so that it matches the first closing-quotation mark it comes across. So:

/title:"(.+?)"/

The parentheses are necessary if you want to extract the title text that it matched out of there.

yjerem
Thanks for the explanation. Works nicely.
Sainath Mallidi
One more question, I do string.match(/title:"(.+?)"/) the it return the entire `tile:<actual_title>`. Is there any cutesy way apart from ugly chomping of characters that I am doing now?
Sainath Mallidi
yeah yeah, that's what the parentheses are for. Your string.match expression will return a MatchData object, which you can index into to get at the matched text inside the parentheses. In your case: `string.match(/title:"(.+?)"/)[1]` should do it.
yjerem
Pretty cool and thanks again.
Sainath Mallidi
A: 

If it helps, you may like rubular.com, a beautiful online regex tester (and a ruby one).

Dave Goodchild
Yeah.. I later saw it, really awesome regex tester
Sainath Mallidi