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
Related
The parameter email_or_phone_number comes from a client side and I need this for an authentication. However, the model has email and phone number fields separately.
What is the right way to parse the email_or_phone_number attribute, so then I could use find_by to find a user?
find_by(email: email, phone_number: phone_number)
If it's number, then it should become phone_number, but if it's not, then that is supposed to be an email.
A simple readable solution for your question would be using an OR query
Mongoid
User.where(email: params[:email_or_phone_number])
.or(phone_number: params[:email_or_phone_number])
.first
UPDATE Active Record
User.where(email: params[:email_or_phone_number])
.or(User.where(phone_number: params[:email_or_phone_number]))
The other method could be using a Regex for identifying Email Format or Phone Number Format of the value in params[:email_or_phone_number] but I have not come across any which are full proof in case of identifying there formats.
You can split email or number with a regex.
x = {email_or_phone_number:"3333"}
x[:email_or_phone_number].match(/^\d+/)
if x is nil, then
x[:email_or_phone_number].match(/\A([\w+\-]\.?)+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i)
else: is not valid input! Save result and pass res to find_by
Assuming that the desired behavior is that searching for 123 matches both greg123#example.com (an email) and 123123123 (a phone number) then the correct query would be:
User.where('email ILIKE :q OR phone ILIKE :q',
q: "%#{params[:email_or_phone]}%")
I'm not sure what your database is but keep in mind that this query may perform poorly on larger datasets. If that's the case I recommend you take a look at pg_trgm (Postgres-only) or full-text search solutions.
Given an email header input like this
Cc: "'Johnny sudson'" <johnny#quvx.com>, <martin#quvx.com>,
<Joe#quvx.com>
how do I get lua to match only the email address? my intuition of it being <(.*)> or something similar only matches in a greedy fashion, where as I need entry individually....
my use case is something like this.
for emails in string.gmatch(all_cc,"<(.*)>" )
do
so I can then work with each email address individually
Non-greedy matching in Lua is represented in your case by <.->.
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.
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 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.