Regex for excluding 855 numbers with or without +1s - regex

I have the following set of strings:
tel:+1 855 345 3455
tel:+185564354
tel:+85523456
tel:1855345445
tel:6047222733
tel:+54434553
tel:+1833453335
I am trying to write a regex that will omit any string value that contains an 855 number that may or may not be preceded by a 1, +, space or a combination of all three.
I tried a few but none seem to give me a 100% accurate match.
The one that seems to work for most strings is: **^tel:[+]?[1]?[ ]?[^8][^5][^5].*$** but it also matches these two string values:
tel:+1 855 345 3455
tel:+185564354
And I am not sure why.
Can any regex whiz help?

Try with following regex.
Regex: ^(?=.*855).*$
Explanation:
(?=.*855) a positive lookahead for 855 anywhere in the string. If present then only whole string will be a match.
Regex101 Demo

An alternative approach is
^[1\s+]*(?:855)[\s0-9]*$
^ is the beginning of the string.
[1\s+]* matches any of those characters 0 or more times.
(?:855) is a non-capturing group that means that 855 must be in the string.
[\s0-9]* matches any character in the class (space or digit) 0 more times.
$ is end of string.
if you don't want spaces following 855 change the character class in 4. to what you want.
working regex

Here's one approach:
tel:(?!\+?1?\s?855).*
tel: <- litteral match
(?!\+?1?\s?855) <- negative look ahead for any numbers starting with +1\s855 where the +1\s are optional, but must be in that order
.* <- match for the rest of the string for strings that aren't caught by the negative lookahead.
https://regex101.com/r/pIcTHA/1

Related

regular expression regex skip special characters such as $ at the front of this specific expression

I have a rather complex regex below but need to make one change to it and that is to skip anytime a special character is found in front. I've tried but can't figure this out myself. Help please.
My regex:
\b\d{3}[\ -]\d{2}[\ -]\d{4}\b|\b\d{2}[\ -]\d{3}[\ -]\d{4}\b|\b\d{4}[\ -]\d{2}[\ -]\d{3}\b|\b\d{9}\b|\b\d{2}[\ -]\d{2}[\ -]\d{5}\b
Example:
Currently it detects all the below values however I need it to skip if a special leading character such as $ or * is present.
123-45-6789
123 45 6789
12 34 56789
123456789
123456788
123456789
123456789-00
You could use this regex:
(?<![\d$*])(?=(?:[ -]?\d){9}\b)\d{2,4}([ -]?)\d{2,3}\1\d{3,5}\b
It matches:
(?<![\d$*]) : a negative lookbehind to assert the preceding character is not a digit, $ or *
(?=(?:[ -]?\d){9}\b) : a lookahead to assert there are exactly 9 digits optionally preceded by a space or -
\d{2,4}([ -]?)\d{2,3}\1\d{3,5} : 2-4 digits, an optional space or -, 2-3 digits, a repetition of the previous optional separator, and 3-5 digits. This ensures that there are exactly 0 or two separators, with minimum and maximum digit group sizes around them.
\b : a word break
Demo on regex101
Have you tried negative zero-length lookahead? If you're trying to avoid astrisks, (?!\*), can do that. It says to find a character that is not the literal '*', match required, but it's not part of the end result. https://www.regular-expressions.info/lookaround.html
I assume you're trying to not match
*123-45-6789
Something like:
\b(?!\*)\d{3}[- ]\d{2}[- ]\d{4}\b|\b\d{2}[- ]\d{3}[- ]\d{4}\b|\b\d{4}[- ]\d{2}[- ]\d{3}\b|\b\d{9}\b|\b\d{2}[- ]\d{2}[- ]\d{5}\b
I tested in https://regex101.com/

Regular expression: Find numbers beginning and ending with different digits

I am looking for a regular expression that can be used to find numbers that begin and end with different digits.
I tried with the following: ^(\d)\d*(?!\1)$
However, this does not work, it gives positive matches for numbers like
121
1233
1441
Where am I getting it wrong? Any ideas?
You could use the positive lookahead (?!\d*\1$) after the capturing group to assert that what follows is not zero or more times a digit ending with group 1:
^(\d)(?!\d*\1$)\d*$
Your regex doesn't actually match the last digit. You should do:
^(\d)\d*(?!\1)\d$
^^
match the last digit!
Your regex just asserts that there isn't the starting digit at the end. Well, an empty string is also "not the starting digit", so it matches things like 1221. You have to tell it to match "a digit that is not the starting digit".
Demo

Reg Exp: match specific number of characters or digits

My RegExp is very rusty! I have two questions, related to the following RegExp
Question Part 1
I'm trying to get the following RegExp to work
^.*\d{1}\.{1}\d{1}[A-Z]{5}.*$
What I'm trying to pass is x1.1SMITHx or x1.1.JONESx
Where x can be anything of any length but the SMITH or JONES part of the input string is checked for 5 upper case characters only
So:
some preamble 1.1SMITH some more characters 123
xyz1.1JONES some more characters 123
both pass
But
another bit of string1.1SMITHABC some more characters 123
xyz1.1ME some more characters 123
Should not pass because SMITH now contains 3 additional characters, ABC, and ME is only 2 characters.
I only pass if after 1.1 there are 5 characters only
Question Part 2
How do I match on specific number of digits ?
Not bothered what they are, it's the number of them that I can't get working
if I use ^\d{1}$ I'd have thought it'll only pass if one digit is present
It will pass 5 but it also passes 67
It should fail 67 as it's two digits in length.
The RegExp should pass only if 1 digit is present.
For the first one, check out this regex:
^.*\d\.\d[A-Z]{5}[^A-Z]*$
Before solving the problem, I made it easier to read by removing all of the {1}. This is an unnecessary qualifier since regex will default to looking for one character (/abc/ matches abc not aaabbbccc).
To fix the issue, we just need to replace your final .*. This says match 0+ characters of anything. If we make this "dot-match-all" more specific (i.e. [^A-Z]), you won't match SMITHABC.
I came up with a number of solution but I like these most. If your RegEx engine supports negative look-ahead and negative look-behind, you can use this:
Part 1: (?<![A-Z])[A-Z]{5}(?![A-Z])
Part 2: (?<!\d)\d(?!\d)
Both have a pattern of (?<!expr)expr(?!expr).
(?<!...) is a negative look-behind, meaning the match isn't preceded by the expression in the bracket.
(?!...) is a negative look-ahead, meaning the match isn't followed by the expression in the bracket.
So: for the first pattern, it means "find 5 uppercase characters that are neither preceded nor followed by another uppercase character". In other words, match exactly 5 uppercase characters.
The second pattern works the same way: find a digit that is not preceded or followed by another digit.
You can try it on Regex 101.

How do I write a regex that won't match a certain amount of whitespace?

I'm trying to write a regex that won't match a certain number of white spaces, but it's not going the way I expected.
I have these strings:
123 99999 # has 6 white spaces
321 99999 # same
123 8888 # has 3 white spaces \
321 8888 # same | - These are the lines I
1237777 | want to match
3217777 /
I want to match the last four lines, i.e. starts with 123 or 321 followed by anything but 6 whitespace characters:
^(123|321)[^\ ]{6}.*
This doesn't seem to do the trick - this matches only the two last ones. What am I missing?
" 888"
If you match this up, this does not match [^\ ]{6}: this is saying
[not a space][not a space][not a space][not a space][not a space][not a space]
In this case, you have the problem that the first 3 characters are a space, so it's not matching up right.
You can use a negative lookahead ^(123)|(321)(?!\s{6}). What I prefer because it is more readable, is to write the regular expression to match what you don't want, then negate (i.e., not, !, etc.). I don't know enough about your data, but I would do use \s{6}, then negate it.
Try this:
^(123|321)(?!\s{6}).*
(uses a negative lookahead so see if there are 6 whitespaces in .* match)
What language are you doing this in? If in Perl or something that supports PCREs, you can simply use a negative lookahead assertion:
^(123)|(321)(?!\ {6}).*
You need to first say that it may have 3 whitespaces and then deny the existence of the three more whitespaces, like this:
^([0-9]+)(\s{0,3})([^ ]{3})([0-9]*)$
^([0-9]+) = Accepts one or more numbers in the beginning of your string.
(\s{0,3}) = Accepts zero or up to three spaces.
([^ ]{3}) = Disallow the next 3 spaces after the allowed spaces.
([0-9]*) = Accepts any number after spaces till the end of your string.
Or:
^([0-9]+)(\s{0,3})(?!\s+)([0-9]*)$
The only change here is that after the three allowed spaces it won't accept any more spaces (I particularly like this second option more because it's more readable).
Hope it helps.

Regex allow a string to only contain numbers 0 - 9 and limit length to 45

I am trying to create a regex to have a string only contain 0-9 as the characters and it must be at least 1 char in length and no more than 45. so example would be 00303039 would be a match, and 039330a29 would not.
So far this is what I have but I am not sure that it is correct
[0-9]{1,45}
I have also tried
^[0-9]{45}*$
but that does not seem to work either. I am not very familiar with regex so any help would be great. Thanks!
You are almost there, all you need is start anchor (^) and end anchor ($):
^[0-9]{1,45}$
\d is short for the character class [0-9]. You can use that as:
^\d{1,45}$
The anchors force the pattern to match entire input, not just a part of it.
Your regex [0-9]{1,45} looks for 1 to 45 digits, so string like foo1 also get matched as it contains 1.
^[0-9]{1,45} looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches 123 but also 123foo
[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be at the end of the input. It matches 123 but also foo123
^[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be both at the start and at the end of the input, effectively it should be entire input.
The first matches any number of digits within your string (allows other characters too, i.e.: "039330a29"). The second allows only 45 digits (and not less). So just take the better from both:
^\d{1,45}$
where \d is the same like [0-9].
Use this regular expression if you don't want to start with zero:
^[1-9]([0-9]{1,45}$)
If you don't mind starting with zero, use:
^[0-9]{1,45}$
codaddict has provided the right answer. As for what you've tried, I'll explain why they don't make the cut:
[0-9]{1,45} is almost there, however it matches a 1-to-45-digit string even if it occurs within another longer string containing other characters. Hence you need ^ and $ to restrict it to an exact match.
^[0-9]{45}*$ matches an exactly-45-digit string, repeated 0 or any number of times (*). That means the length of the string can only be 0 or a multiple of 45 (90, 135, 180...).
A combination of both attempts is probably what you need:
^[0-9]{1,45}$
^[0-9]{1,45}$ is correct.
Rails doesnt like the using of ^ and $ for some security reasons , probably its better to use \A and \z to set the beginning and the end of the string
For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):
\b\d{1,45}\b
\b is a position between \w and \W (non-word char), or at the beginning or end of a string.