How do one code an email, say, alibaba#gmail.com to a***a#g***l.c*m in ruby on rails?
I found this when I tried to recover my password to my gmail account.
If you have the email already split into address and domain this is much easier. But to do that its simply:
email = 'alibaba#gmail.com'
address, domain = email.split('#')
If you don't care about the character count between the first and last of each token:
"#{address[0]}***#{address[-1]}"
for the a**a before the # and similar can be done for the domain but using split on the . character:
working_domain = domain.split('.')
"#{working_domain[0][0]}***#{working_domain[0][-1]}.#{working_domain[1][0]}*#{working_domain[1][-1]}"
That's a pretty ugly way to do it and its not very DRY and doesnt care about character counts. You should be able to encapsulate all of this into a function or 3 and make this simpler to use.
From the example you give in the question ("alibaba#gmail.com" => "a***a#g***l.c*m"), it appears you don't need the number of *'s to match the number of replaced characters. If that's the case, you can solve this with a simple regex substitution, no splitting or parsing of the address necessary:
email = 'alibaba#gmail.com'
email.gsub(/(?<=[\w\d])[\w\d]+(?=[\w\d])/, "**")
# => "a**a#g**l.c**m"
Breaking down that regex, just for clarity: [\w\d]+ matches strings of alphanumeric characters, excluding one alphanumeric to the left ((?<=[\w\d])) and another to the right ((?=[\w\d])) of each matched group, and replaces each match with "**".
I hope this helps.
Related
I need an regular expression which accept 10 digit phone number and also email address with one input textfield.
Example :
Phonenumber: 1234567890
Emailaddress : somename#somecompany.com or somename#somecompany.in
Can any one advice me to get that regular expression.
#KiranMac123
Use the below regex to match the 10 digit phone number or an email address.
^(?:\d{10}|\w+#\w+\.\w{2,3})$
I would simply do this:
^(.+#.+|\d{10})$
Note that this regex does not match all valid email addresses. And it does match some that are not valid. But for most cases it's good enough.
Unless you have a good reason, I would strongly advice against using a regex to check email addresses. Read here why
I use something like this:
^(?:0\d{9}|\w+[-,_,.,\w]\w+#\w+.\w{2,3})$
It takes into consideration emails like:
word.word#word.dom
and
word-word#domain.com
Here's the explanation: Check on imgur
I want to match emails separated by a comma like this :
'a#a.com, b#b.com, c#c.com'
I'm would like to reuse the regex defined in the Devise gem :
/\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z/
I know that I won't be RFC2822 compliant but I don't care because I only need a very simple email validation.
My attempt to match the emails separated by commas is the following :
/\A(([^#\s]+#([^#\s]+\.)+[^#\s]+),)*\s([^#\s]+#([^#\s]+\.)+[^#\s]+)\z/
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
everything like 'a#a.com,' spaces last element 'c#c.com'
many times
Unfortunately this is not working as expected. It is matching string like this (note the trailing comma) :
a#a.com, b#b.com, c#c.com,
Could someone help me with this please?
You are only matching the space after the last comma, move the \s to the group that is repeating. Also as Platinum Azure mentioned [^#\s] includes commas you can avoid this by changing it to [^#\s,]
/\A(([^#\s]+#([^#\s]+\.)+[^#\s,]+),\s)*([^#\s]+#([^#\s,]+\.)+[^#\s,]+)\z/
I have an Application model which has app_id and secret_key fields. What is the best way to generate unique app_ids?
I can use ActiveSupport:SecureRandom.hex(16) to generate an 32-char alpha-numeric string and there will probably be no other string like it. If done in this manner, should I also do a database check to see if there is a duplicate app_id or is this step unnecessary since the likelihood of that is infinitesimally small?
Or is there a better method?
Thanks!
Tim
I would always double check, just to be sure. Put a unique index on app_id and it's all set. It's hard to guarantee uniqueness
However, you could build a string that is guaranteed to be unique.
string = ActiveSupport::SecureRandom.hex(16)
append = #app.id.to_s
string = string.slice(0, string.length - append.length) + append
So the first part is random, but it always ends with the database id column, which would have to be unique.
There are also likely variations of this that keep the random looking structure, e.g. using a Caesar Cipher or a simple numeric to alphabetic cipher.
I would check first.
Here's some code I've seen in devise used when generating a unique token (modified for your example):
loop do
token = ActiveSupport::SecureRandom.hex 16
break token unless find(:first, :token => token)
end
Line 162:
https://github.com/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb
I want to use validates_format_of to validate a comma separated string with only letters (small and caps), and numbers.
So.
example1, example2, 22example44, ex24
not:
^&*, <> , asfasfsdafas<#%$#
Basically I want to have users enter comma separated words(incl numbers) without special characters.
I'll use it to validate tags from acts_as_taggable_on. (i don't want to be a valid tag for example.
Thanks in advance.
You can always test out regular expressions at rubular, you would find that both tiftiks and Tims regular expressions work albeit with some strange edge cases with whitespace.
Tim's solution can be extended to include leading and trailing whitespace and that should then do what you want as follows :-
^\s*[A-Za-z0-9]+(\s*,\s*[A-Za-z0-9]+)*\s*$
Presumably when you have validated the input string you will want to turn it into an array of tags to iterate over. You can do this as follows :-
array_var = string_var.delete(' ').split(',')
^([a-zA-Z0-9]+,\s*)*[a-zA-Z0-9]+$
Note that this regex doesn't match values with whitespace, so it won't match multiple words like "abc xyz, fgh qwe". It matches any amount of whitespace after commas. You might not need ^ or $ if validates_format_of tries to match the whole string, I've never used Rails so I don't know about that.
^[A-Za-z0-9]+([ \t]*,[ \t]*[A-Za-z0-9]+)*$
should match a CSV line that only contains those characters, whether it's just one value or many.
The url of our search page is build like this:
http://www.example.com/results/name/John/city/Miami/gender/Male
This would display every male named John in Miami.
When one of the filters is left empty, the url would be something like this:
http://www.example.com/results/name/John/city//gender/Male
So there are two slashes in the url.
Outlook doesn't seem to like this. When you click on the second url, it removes one of the two slashes. This leaves the url like this:
http://www.example.com/results/name/John/city/gender/Male
Persons names John in the city 'gender'..
What would be the best way to fix this problem?
This is a bug in Microsoft Office.
URLs with two consecutive slashes are allowed by RFC 2396, but they're not commonly used. As the RFC says (extract from Appendix A):
abs_path = "/" path_segments
path_segments = segment *( "/" segment )
segment = *pchar *( ";" param )
Note that segment is defined as containing ZERO OR MORE characters. (You might argue that this is a spec bug, and it shouldn't be allowed... but it is)
As you've discovered, Microsoft Office will "fix" URLs containing double slashes. This is apparently a deliberate feature for "cleanliness and consistency". There is no way to override or disable it. Source.
So, as other people have suggested, you're probably going to have to change the way the server formats URLs.
Try to replace (one of) the slashes with ASCII code 2F (decimal 47).
"Regkey" should help: You need to implement this key
Path: "HKEY_CURRENT_USER\Software\Microsoft\Office\Common"
Name: "AllowConsecutiveSlashesInUrlPathComponent"
Type: "REG_DWORD"
Value: "1"
For implementation & more detail see here.
The standard is to collapse the two slashes into one, so there is no way to prevent this from happening. It might be a good idea to put something between those slashes to indicate to your search page that that field is blank.
Alternatively, you could change the search page to use a query string such as this:
http://www.example.com/results?name=John&city=&gender=Male
You can use - instead of blank segment. For example:
http://www.example.com/results/name/John/city/-/gender/Male
I agree with Peter, just replace the second "/" with "%2F" or "%2f" is enough.
If you must use slashes, consider fixing this on the server side. Create a list of keywords (city|results|...) and, if a slash is followed by one of the keywords, treat it as an empty entry. (edited) Double slashes should not be treated as one, but as you found out, some applications "fix" this.
An alternative and standard way of fixing this is using a placeholder, normally a dot, because it has no special meaning:
http://example.com/results/name/./city/amsterdam