How to get regex to ignore URL strings? - ruby-on-rails

I have the following Regexp to create a hash of values by separating a string at a semicolon:
Hash["photo:chase jarvis".scan(/(.*)\:(.*)/)]
// {'photo' => 'chase jarvis'}
But I also want to be able to have URL's in the string and recognize it so it maintains the URL part in the value side of the hash i.e:
Hash["photo:http://www.chasejarvis.com".scan(/(.*)\:(.*)/)]
// Results in {'photo:http' => '//www.chasejarvis.com'}
I want, of course:
Hash["photo:chase jarvis".scan(/ ... /)]
// {'photo' => 'http://www.chasejarvis.com'}

If you only want to match up to first colon you could change (.*)\:(.*) to ([^:]*)\:(.*).
Alternatively, you could make it a non-greedy match, but I prefer saying "not colon".

How do figure out a person's family name and first name?
Changing chasejarvis to chase and jarvis might not be possible unless you have a solution for that.
Do you already know everyone's name in your project? Nobody is having the initial of a middle name like charvisdjarvis (assuming the name is "Charvis D. Jarvis".)?

Related

Updating data in string with Lua

There are different urls in user_data, I want to change all usernames and passwords in user_data to make them the same. Since the URL directories are different, I want to find the username and password first, starting from the end, and update it with the new username and password.
username and password I want to change: "superuser/123456"
johndoe/123h456 => superuser/123456
james/98c7654 => superuser/123456
robert/32165g4 => superuser/123456
What do you think is the surest and most accurate way to do this using match or gsub ?
local user_data = "http://127.0.0.1/johndoe/123h456/1, http://127.0.0.1/news/james/98c7654/1.jpg, http://127.0.0.1/data/robert/32165g4/1.dat"
Assuming there is only one URL per string and assuming you can anchor the string on the right side (so the username/password are the last path elements), I'd try something like this: user_data:gsub("/[^/]+/[^/]+(/[^/]+)$","/superuser/123456%1").
$ anchors the replacement at the end of the string, [^/]+ means one or more characters, but not /, and %1 replace it with the first capture (which is a first expression in ()).

how to join channels with a variable username (mirc)

simple question, i guess, but I'm fairly new to this, so maybe you can help me.
I made a bot for a chat, which i want to share with people.
So, my first thought was: i'll add a command (!join), which then will let the bot
join the specific channel. For some reason (i guess its due the operators), my join won't work.
Here is the snippet:
on *:TEXT:!join:#: {
var %name = $nick
;/msg $chan joining channel %name
/join #%nick
}
But it just won't connect. Any ideas?
If i just use /kick $nick (or %name), it works, so i guess this # is messing things up.
Thanks in advance
Try the following:
/join $chr(35) $+ %nick
Explanation: A variable name must be a word on its own in your line of code for it to be recognised as a variable name. Therefore, #%nick will be interpreted as the string #%nick, whereas %nick will be interpreted as the name of the user issuing the command.
To append the value of a variable or identifier, you can use the identifier $+ which appends strings together. For instance, a $+ b will return ab.
Another issue arises when you use # $+ %nick, because # is an alias for the identifier $chan. This means that if I were to type !join in #test, it would try to join #testPatrickdev. Instead of using #, I use $chr(35) (which will in turn return the character #). It appends that to the value of the variable %nick.
Use mIRC's $eval function, eg: $($+(#, %nick))

Scanning text to find domain types

I'm trying to piece together a part of a create action in a controller that scans the the text entered and intelligently understands what type of domain name it is.
I have a text box called "domain_names". A user puts domains into the box separated by commas, e.g. "yahoo.com, google.com"
In the controller it hits it like this:
#extracted_domains = (params[:domain_names]).split(",")
#extracted_domains.each do |domain|
domain.strip
domain_scan = domain.scan(/(\w+)[.]/).flatten
com_scan = domain.scan(/[.](\w+)/).flatten
new_domain_type = DomainType.find_or_create_by_domain_type(:domain_type => com_scan)
new_domain = Domain.create(:domain => domain_scan, :domain_type_id => new_domain_type.id)
end
In the console it works great. But when I put it into practise I get really odd things stored in the database. For example if :domain was meant to have the value "google", it will instead have the value "---\n- google\n" , when its stored in the database.
No idea why
Thanks in advance.
UPDATE**
Problem: It was putting an array into a string.
Solution: Make it a string.
domain_scan = domain.scan(/(\w+)[.]/).flatten.first
com_scan = domain.scan(/[.](\w+)/).flatten.first
It appears to be fed YAML input. Three dashes at the beginning of the string followed by a newline are a strong indicator of YAML: http://en.wikipedia.org/wiki/YAML#Sample_document
As for your issue, can we see the exact params that are sent?
I would take a look at https://github.com/pauldix/domainatrix for domain extraction.

extract information from particular string

I am new to ruby on rails. I have passed parameters ISDCode,AreaCode and Telephone number using POST from a form.
I have a string with information of the format countryName(ISDCode) passed in the variable ISDCode. For example "United States of America(+1)".
Now I want to save only the value of the ISDCode in the database.
What would be the ideal way to extract the ISD Code from the string?
Should I extract the ISD Code in Javascript before user POSTs the form or should I extract it in the model using a callback ?
Also is regex the only way to extract the information?
Since the string is from auto completion, the ISDcodes should be existing in your database. So the best solution may be including an extra parameter (with a hidden input), like isdcode_id, then you simply use isdcode_id in your model. This way you can avoid the trouble to parse the string.
If this is not feasible, regex could be the best way to extract the information. You can override the setter in the model to do it.
use regular expression to match ISDcode
"United States of America(+1)" =~ /(\+[\d]+)/
puts $1
If you are interested in getting just the ISD Code alone, this should work:
"United States of America(+1)".gsub!(/[^\+\d]/, "")
NB: You can have this in your view helper and just call the helper on the string before persistence
Already answered, but I'd like to offer an alternative to getting the ISD Code:
isd = "United States(+1)"
puts isd[/[+]*[\d]{1,4}/] # +1
This regexp matches:
0001
+1
+01
etc.
I prefer to use js to extract information in the client side and make a validation in the model. By this way, you can get what you want and make sure it's correct.

Prevent spaces in URL showed like %20

I would like to display nice URL in address bar and avoid spaces to display like %20
Here is an example:
Is it possible to replace these spaces with a dash ?
Something like: /BANQUE/International-Ledger
Maybe something to do in the routing ?
Thanks.
You dont want to replace spaces in generated route, you want to not generate them in the first place.
What is your "International Ledger" ? If it is action, then use [ActionName("International-Ledger")]
If it is some kind of product or category of product, is it good practice dont use product name for URL, but some "token" generated from name, for example with regex, replacing spaces with dashes, special letters with theirs basic alphabet variants, and maybe some unique identifier to prevent conflicts of products with the same name.
see How can I create a SEO friendly dash-delimited url from a string?

Resources