regular expression pattern not matching with my string - regex

How can I rewrite below regular expression
/((?:\+|00)[17](?: |\-)?|(?:\+|00)[1-9]\d{0,2}(?: |\-)?|(?:\+|00)1\-\d{3}(?: |\-)?)?(0\d|\([0-9]{3}\)|[1-9]{0,3})(?:((?: |\-)[0-9]{2}){4}|((?:[0-9]{2}){4})|((?: |\-)[0-9]{3}(?: |\-)[0-9]{4})|([0-9]{7}))/g
for matching with this pattern
+1(555)532-3455

Your expression seems needlessly complicated for matching that string. You're trying to match the following aspects:
a plus sign with a country code
an area code wrapped in brackets
the first 3 digits of the phone number
a dash
the last 4 digits of the phone number
no spaces anywhere
To match this you only need
^\+[0-9]\([0-9]{3}\)[0-9]{3}\-[0-9]{4}$
If you need to allow for optional spaces between the country code, area code and phone number you can add them to the expression.
^\+[0-9]\s?\([0-9]{3}\)\s?[0-9]{3}\-[0-9]{4}$
(I may have unnecessary escapes \ in there)

Alternatively you can do:
^\+?\d(?:\s*(?:\(|\-)?\s*)\d{3}(?:\s*(?:\)|\-)?\s*)(?:\d{3}(?:\s*\-?\s*)?\d{4})$
See the regex demo for some examples of what this regex can and can not match.

Related

Regular Expression allow only numbers, commas and dashes

I'm trying to come up with a Data Annotation regular expression to match the following formats.
34
38-30
100,25-30
4-5,5,1-5
Basically the expression should only allow numbers, -(dash) and ,(comma) in any order
I tried following but couldn't get it working.
[RegularExpression(#"(0-9 .&'-,]+)", ErrorMessage ="Lot numbers are invalid.")]
It's ^[0-9,-]*$. Check out this demo.
I think your use case is having a CSV list of numbers, or ranges of numbers (identified as a number followed by a dash followed by another number). We can use the following regex:
[0-9]+(?:-[0-9]+)?(,[0-9]+(?:-[0-9]+)?)*
This regex matches a number, followed by an optional dash and another number, that quantity then followed by comma and another similar term, any number of times.
In the demo below I added anchors on both sides of the regex. Whether you need to do this depends on how you plan to use the pattern.
Demo

Match only exact numbers, not pre of suffixed with slash/dash etc

I need a regular expression that matches only numbers of length 7 (they can have leading zeros). I used the following super easy regex: \b[0-9]{7}\b. However, this regex also matches numbers in e.g. 5254-6408499 and (0241)4013999 (see https://regex101.com/r/zF5hV7/1).
How can I prevent them from being matched? I only want numbers of length 7 having leading and/or trailing spaces.
Depending on the regular expression flavor, you could create your own boundaries:
(?<=^| )\d{7}(?= |$)
This asserts that either the beginning of the string or a space precedes moving on to matching exactly 7 digits only if the engine asserts that either a space or the end of string follows.
You can use this regex:
(?:^|\s)([0-9]{7})(?:\s|$)
and grab captured group #1
Updated RegEx Demo

Regular Expression for email to check repetitive characters

I'm validating email address with regular expression. I would like to test for a following conditions:
minimum of 3 characters in name, symbol #, minimum 3 characters in first part of domain, a dot,no more than 3 repetitive characters
I tried this regular expression and it's working fine for all cases except last one.
/^[A-Za-z0-9._%+-]{3,}\#[A-Za-z0-9.-]{3,}\.[A-Za-z]{2,4}$/
It's not checking for repetitive character(any character) after dot(.)
Not Ok: test#test.ccccom, test#test.coooom
Ok : test#test.com
Don't know what is wrong with last portion of my RE.
Any input will be appreciated.
You can use the following regex:
^(?!.*([A-Za-z0-9])\1{3})[A-Za-z0-9._%+-]{3,}\#[A-Za-z0-9-]{3,}\.[A-Za-z]{2,4}$
Changes made:
(?!.*([A-Za-z0-9])\1{3}) - This is a negative lookahead that makes sure that none of the characters repeat more than thrice in a row.
The rest of the regex is same as it is, except for the removal of the . from the second character class.
RegEx Demo
If you want to disallow repeated characters after the last ., then you could use the following instead:
^[A-Za-z0-9._%+-]{3,}\#[A-Za-z0-9-]{3,}\.(?!([A-Za-z0-9])\1{3})[A-Za-z]{2,4}$
RegEx Demo
This won't allow more than three repeated characters after the last dot,
^[A-Za-z0-9._%+-]{3,}\#[A-Za-z0-9.-]{3,}\.(?:(?!(.)\1{3})[a-zA-Z]){2,4}$
DEMO

Regular expression to correct email address

I need help in writing one regular expression where I want to remove unwanted characters in the start and end of the email address. For example:
z>user1#hotmail.com<kt
z>user2#hotmail.pk<kt
z>puser3#yahoo.com<kt
z>npuser4#yaoo.uk<kt
After applying regular expression my emails should look like:
user1#hotmail.com
user2#hotmail.pk
puser3#yahoo.com
npuser4#yaoo.uk
Regular expression should not applied if email address is already correct.
You can try deleting matches of
^[^>]*>|<[^>]*$
(demo)
Debuggex Demo
Find ^[^>]*>([^<]*)<*.*$ and replace it with \1
Here's an example on regex101
I think you might be missing the point of a regular expression slightly. A regular expression defines the 'shape' of a string and return whether or not the string conforms to that shape. A simple expression for an email address might be something like:
[a-z][A-Z][0-9]*.?[a-z][A-Z][0-9]+#[a-z][A-Z][0-9]*.[a-z]+
But it is not simple to write one catch-all regular expression for an email address. Really, what you need to do to check it properly is:
Ensure there is one and only one '#'-sign.
Check that the part before the at sign conforms to a regular expression for this part:
Characters
Digits
Extended characters: .-'_ (that list may not be complete)
Check that the part after the #-sign conforms to the reg-ex for domain names:
Characters
Digits
Extended characters: . -
Must start with character or digit and must end with a proper domain name ending.
Try using a capturing group on anything between the characters you don't want. For example,
/>([\w|\d]+#[\w\d]+.\w+)</
Basically, any part that the regexp inside () matches is saved in a capturing group. This one matches anything that's inside >here< that starts with a bunch of characters or digits, has an #, has one or more word or digit characters, then a period, then some word characters. Should match any valid email address.
If you need characters besides >< to be matched, make a character class. That's what those square bracketed bits are. If you replace > with [.,></?;:'"] it'll match any of those characters.
Demo (Look at the match groups)

Regular Expression for matching a phone number

I need a regular expression to match phone numbers. I just want to know if the number is probably a phone number and it could be any phone format, US or international. So I developed a strategy to determine if it matches.
I want it to accept the following characters: 0-9 as well as ,.()- and optionally start with a + (for international numbers). The string should not match if it has any other characters.
I tried this:
/\+?[0-9\/\.\(\)\-]/
But it matches phone numbers that have + in the middle of the number. And it matches numbers that contain alpha chars (I don't want that).
Lastly, I want to set the minimum length to 9 characters.
Any thoughts?
Thanks for any help, I'm obviously not too swift on RegEx stuff :)
Well, you're pretty close. Try this:
^\+?[0-9\/.()-]{9,}$
Without the start and end anchors you allow partial matching, so it can match +123 from the string :-)+123.
If you want a minimum of 9 digits, rather than any characters (so ---.../// isn't valid), you can use:
^\+?[\/.()-]*([0-9][\/.()-]*){9,}$
or, using a lookahead - before matching the string for [0-9/.()-]* the regex engine is looking for (\D*\d){9}, which is a of 9 digits, each digit possibly preceded by other characters (which we will validate later).
^\+?(?=(\D*\d){9})[0-9\/.()-]*$
The reason why it matches alpha character is because of the period. You have to escape it. I don't know what editor you are using for this, this is what I'll use for VIM:
^+\?[()\-\.]\?\([0-9][\.()\-]\?\)\{3,\}$
The juqeury has a plugin for US phone validation. Check this link. You can also see the regular expression in the source code.