Hi i have field called UserName into my MVC 3 Model. This propery could be valid email address or could be valid AlphaNumeric. How i can write Regex Express to check both things (Valid Email or Valid Alpha Numeric).
Valid Email Expresion which i am using is
"^[_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))$"
Assuming that the alphanumeric string you want to match instead of an email address really is just A-Z and 0-9, you can take what you have already...
^[_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))$
...and wrap it in parenthesis with a pipe to indicate it as an optional group...
^([_a-zA-Z0-9\'-]+(\.[_a-zA-Z0-9\'-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(name))|[a-zA-Z0-9]+)$
Which seems like an overly simplistic solution, give the work you have done so far, which makes me wonder if there is some catch to this???
if you want alpha numeric(must contain single alpha and single numeric) try this
(?=[0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*)(?=[0-9a-zA-Z]*[0-9][0-9a-zA-Z]*)(^[0-9a-zA-Z]+$)
Related
I have a Flutter TextFormField for email with input formatter as below.
var emailAddressFormatter = FilteringTextInputFormatter.allow(RegExp(
r"[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+#[a-zA-Z0-9]+\.[a-zA-Z]+"));
The problem is, when trying to input any character in the field it does not allow. The regex looks fine to me. When the formatter is removed the field accepts any character with any format. What am I going wrong?
The issue is that the FilteringTextInputFormatter that you're using rejects anything that does not match your regex. When you enter just a single character, it does not match your regex, so the character is rejected.
I know little about regex so I'm not sure if it's possible, but you would need a regex that would be able to match every string as you type e.g. a, am, amani#, amani#gmail.com.
I would personally not try to do filtering such as this. Instead, I would just allow all valid characters that are valid in email addresses to be present in the email and not enforce the specific format with the # and .. Then I would use a validator to check that the email is valid upon form submission.
If you don't like the alternate solution I proposed above and you can't use regex, you can make your own input formatter quite easily with TextInputFormatter.withFunction.
I have a text field which stores a list of email addresses e.g: x#demo.com; a.x#demo.com. I have another text field which stores the exact value matched from the list of emails i.e. if /x#demo.com/i is in x#demo.com;a.x#demo.com then it should return x#demo.com.
The issue I am having is that if I have /a.x#demo.com/i, I will get x#demo.com instead of a.x#demo.com
I know of the regex expression /^x#demo.com$/i, but this means I can only have one email in my list of email addresses which won't help.
I have tried a couple of other regex expressions with no luck.
Any ideas on how I can achieve this?
You can use this slightly changed regex:
/(^|;)x#demo.com($|;)/i
It will match from either beginning of string or start after a semi colon and end either at end of string or at a semi colon.
Edit:
Small change, this uses look behind and look forward, then you will only get the match, you want:
(?<=^|;)x#demo.com(?=$|;)
Edit2:
To allow Spaces around the semi colon and at start and end, use this (#-quoted):
#"(?<=^\s*|;\s*)x#demo.com(?=\s*$|\s*;)"
or use double escaping:
"(?<=^\\s*|;\\s*)x#demo.com(?=\\s*$|\\s*;)"
I try to match the username of users on YouNow from a specific field.
I extracted this html, I try to extract the username _You Won
"\n\t\t\t\t\t\t14\n\t\t\t\t\t\t_You Won\n\t\t\t\t\t"
This is my regex attempt:
(\d+)[\\n\\t]+([\W\w]+[^\\n\\t"$])
This worked fine, first I match a number which is the level, then I match the username. However, if the username ends with either t or n then it does not get the last letter. So user game 1n would get cut down to game 1
Does someone know how I can fetch the username correctly?
Play it:
https://regex101.com/r/j8rufa/2
You could use Positive Lookahead at the end instead of [^\\n\\t"$].
Your code will be:
(\d+)[\\nt]+([\W\w]+(?=\\n\\t))
Demo: https://regex101.com/r/j8rufa/4
You can also use Positive Lookbehind to further enhance the code to ensure that the whole name is matched. For example, if the name is something like t_You Won, it will be matched without any issues:
(\d+)[\\nt]+(?<=\\t)([\W\w]+(?=\\n\\t))
Demo: https://regex101.com/r/j8rufa/6
Currently i am writing a software where a user can input more than one email in a input field separated by: ";"
Now i have a regex that validates the email but sadly enough doesn't work when i have more Emails in the input field when using the separation.
Has anyone ever created such a regex or is there anyone that is able to help me?
Thanx in advance and looking forward for a response.
Here is my Regex:
[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(\;|)
Just put the pattern which matches the following emails inside a non-capturing group with a preceding ; and make it to repeat zero or more times.
^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+(?:;[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]{2,4}+)*$
And one more thing is, you need to escape the dot.
Is there a way to differentiate between an email address and a twitter id?
Both use the '#' character and the email regex will be contained by the twitter id regex.
What's the best way to approach this?
Should I require a whitespace before the '#' character in order to identify that it's a twitter id?
Not entirely sure which characters are allowed in twitter usernames, but basically like so:
/(?:^|\s)#[a-zA-Z0-9_.-]+\b/
You can test that it's preceded by whitespace using (?<=\s) and then check for the valid characters of twitter IDs which are only [A-Za-z0-9_].
That gives you a resulting regex of: (?<=\s|^)#[A-Za-z0-9_]+
You could eventually add a check for a dot, comma or whitespace after it to check that it's properly formatted within a sentence and not some weird artifact:
(?<=\s|^)#[A-Za-z0-9_]+(?=[\s.,])
Note that the lookbehind and lookahead (?<= and ?=) might not work in your language of choice, but I'll assume it does since you didn't specify.
Email addresses never start with an #, while twitter ids always do.
isTwitter = address[0] == '#'
A twitter id wouldn't pass an email regex check.
Regular email:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$
twitter won't have the last characters:
^#[A-Za-z0-9_]+$
So check if it's a valid email, if not, check if it's a valid twitter ID
Farther reading:
How to Find or Validate an Email Address