This question already has answers here:
Regex: match everything but a specific pattern
(6 answers)
Closed 2 years ago.
^-?[0-9][0-9]{0,2}$
I have this regex but numbers but it allows -0, Any solution?
You may use a negative lookahead assertion to disallow certain match:
^(?!-0$)-?[0-9][0-9]{0,2}$
RegEx Demo
Take note of (?!-0$) that says fail the match if we have -0 and end anchor after matching start anchor ^.
Related
This question already has answers here:
Regex: negative lookbehind AND negative lookahead
(3 answers)
Closed 5 months ago.
so I have this regex:
(?<![a-zA-Z])(0)(?![a-zA-Z])
it caches "0"s that don't have a letter before AND no letter after
I want to catch "0"s that either don't have a letter before OR don't have a letter after.
Regex101
You could use the OR operator |:
(?<!\w)0|0(?!\w)
regex101
This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I want true for these cases:
.123
.000
.999
And want false for these cases:
123
a123
.123a
.1234
a.123
This is my current regex:
match, _ := regexp.MatchString("[.]{1}[0-9]{3}", ".123a")
fmt.Println(match)
But this pattern doesn't return false for:
.123a
.1234
a.123
What is the correct regex?
The pattern is as simple as:
^\.\d{3}$
Same as:
^\.[0-9]{3}$
Which is:
^ // from the beginning
\. // a single dot
\d{3} // a digit (exactly 3 times)
$ // until the end of the string
You have to escape the \ symbol though so: ^\\.\\d{3}$
Regexp Demo. Go Demo.
You are close, try restricting the match with ^[.]{1}[0-9]{3}$
See online demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to match (extract) any character/symbol at the 3rd position of a string using regex. And no, I can't use substrings for this scenario. Below are examples I want matched:
ABCDEF => C
123456 => 3
A B C => B
I'm also being guaranteed to have a string of more than 10 characters so I don't have to worry about being less that 3 characters.
Any help would be appreciated.
You may use this regex with a negative lookbehind:
(?<=^..).
RegEx Demo
RegEx Details:
(?<=^..): Lookbehind assertion to match any 2 characters at line start
.: Match character at 3rd position
This question already has an answer here:
Regex return seven digit number match only
(1 answer)
Closed 4 years ago.
I have a string: 12345 XD Hi12345678ab666666cd987654321.
I want to obtain 12345678 and 666666 and discard 98765432 or 87654321, which I could do that by lookahead and lookbehind like (?<!\d)\d{6,8}(?!\d).
The problem is VBA do not support lookbehind.
How do I rewrite the pattern to obtain what I want?
A simple alternative would be to match \D and use a capturing group for the numbers.
(?:\D|^)(\d{6,8})(?!\d)
Demo
You can access the first capturing group with match.Submatches(0)
This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 8 years ago.
Should match the following:-
(222)-333-4444
(010)-123-3435
(100)-454-6565
But Should disallow:-
(000)-000-0000
The only problem is when the input is entered all zeros. Otherwise my regex:-
^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$
works fine__.
Demo
http://regexstorm.net/tester
You can use this negative lookahead based regex:
^(?![0()-]+$)\(?(\d{3})\)?[-. ]?(\d{3})[-. ]?(\d{4})$
RegEx Demo
(?![0()-]+$) is a negative lookahead that says fail the match if matches only [0()-] character till the end.