Tag: regex

How to replace a pattern in a string

I built a page that is rendered differently depending on params. The logic is something like this: <% if params[:x] == “1” %> <!–render version A–> <% elsif params[:x] == “2” %> <!–render version B–> <% elsif params[:x] == “3” %> <!–render version C–> <% end %> I want each version to have two links […]

How do I get a selection from the output of a grep

I have the following text in a file : <img id=”img_1″ style=”display: none” src=”Logs/P2P2014-04-10_14-24-49.txt”/></span></div></div><script type=”text/javascript”>document.getElementById(‘duration’).innerHTML = “Finished in <strong>1m31.846s seconds</strong>”;</script><script type=”text/javascript”>document.getElementById(‘totals’).innerHTML = “1 What I want to do is obtain the stuff after the src i.e. Logs/P2P2014-04-10_14-24-49.txt. I tried the following and put it into a variable in ruby or so : I tried doing […]

Check if string contains only floats separated with commas in Ruby

How can I check if string contains only floats separated by commas with no space. Something like below: str = “0.0687987167581341,0.120311605902415,89.8399554017928,198.151088713489” #true str = “0.068798716758f1341,0.120311605902415, 89.8399554017928,198.151088713489” #False because of “f” in the first value. str = “0.0687987167581341 0.120311605902415” # False because of no space and comma. Basically, how can I check if a string is […]

Can anyone help me DRY this REGEX?

This is my first question (although I have found many perfect solutions to questins on Stack Overflow in the past – it is my first source for help). I have text strings that contain a month and a series of date. Sometimes, there are two months in the string. date1 = “January 9, 10, 15, […]

Regular Expression not working correclty

I am working on regular expressions. id: 1 food: apple, banana id: 2 food: orange ids: 3 food: apple, banana id: 4 food: hello, yellow id: 5food: apple, banana id:6 food: ,,,yellow This is my regular expression code: pattern = /id[:] [[:digit:]]+ food[:] [a-z,]+/ id: 1 food: apple, banana id: 2 food: orange id: 4 […]

ruby regex scan vs .split method

I was trying to build a method that you take the first letter of every word and would capitalize it. I wrote it as def titleize(name) name.scan(/\w+/) { |x| x.capitalize! } end and it just wouldn’t work properly. It wouldn’t capitalize and letters. I did some searching and found the answer here Capitalizing titles eventually. […]

How can I extract URLs from HTML content with a Ruby regexp?

This is an example since it is not easy to explain: <li id=”l_f6a1ok3n4d4p” class=”online”> <div class=”link”> <a href=”javascript:show(‘f6a1ok3n4d4p’,’random%20strings%204′,%20’site2.com’);%20″ onclick=”visited(‘f6a1ok3n4d4p’);” style=”float:left;”>random strings – 4</a> <a style=”float:left; display:block; padding-top:3px;” href=”http://www.webtrackerplus.com/?page=flowplayerregister&amp;a_aid=&amp;a_bid=&amp;chan=flow”><img border=”0″ src=”/resources/img/fdf.gif”></a> <!– a class=”none” href=”#”>random strings – 4 site2.com – # – </a –> </div> <div class=”params”> <span>Submited: </span>7 June 2015 | <span>Host: </span>site2.com </div> <div […]

Consistently separate values in array

After uploading a file I put each line of the file into an array. I want to only save a part of the array, here is an example of the array… [ “1, \”Hlavní\”\n”, “2, OK\n”, “3618, \”Duplicitní záznamy\”\n”, “3619, \”Anyth1ng_ Go@es /n th7s’me??\”\n” ] I want to trim the strings down to… [ “Hlavní”, […]

Ruby select on array of arrays filtering with regex containing variable

examp = [[“packed”], [“crud”], [“paced”], [“it”], [“emo”], [“wrote”], [“pcd”], [“ppcd”], [“pcd”]] word = ‘pcd’ foo = examp.select { |a| a[0][/[aeiou#{word}]/] } p foo Expected output: [[“paced”], [“pcd”]] Actual output: [[“packed”], [“crud”], [“paced”], [“it”], [“emo”], [“wrote”], [“pcd”]] Edit: [“ppcd”] and [“pcd”] (second time) added to the array. I forgot to mention that I also want to […]

How do I improve regex to eliminate unnecessary method chaining?

This functional method takes a number and returns the same value separated with commas, as is the common convention in the US. The only way I could get it to work with regex was to reverse the string before and after the expression. Is there a regex that can help me eliminate the need to […]

Ruby is the best programming language in the world - Ruby on Rails.