How regex match exact 3 numbers [duplicate] - regex

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

Related

REGEX for Numbers from -999 to 999 , but not -0 [duplicate]

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 ^.

REGEX: match all numbers if a keyword exists [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to find a regex which searches for every numbers if a keyword exists in the paragraph.
For example, if my keyword is something, with this paragraph:
20
30
abc
40
def
something
my regex should get 20, 30 and 40. But for this one:
50
60
xyz
it should get nothing.
Can you guys help me out to find a good regex. Thank you so much! I'm using PCRE
You can use this regex in single line (DOTALL) mode with a lookahead assertion:
(?s)\b\d+(?=.*\bsomething\b)
It will match numbers only when there is a word something ahead in input.
RegEx Demo
RegEx Details:
(?s): Enable single line mode so that dot also matches newlines
\b: Match a word boundary
\d+: Match 1+ digits
(?=.*\bsomething\b): Positive lookahead to assert that we have a word something ahead of us from current position

RegEx to match any character at the 3rd position [duplicate]

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

If Else regex matching [duplicate]

This question already has answers here:
regular expressions: match x times OR y times
(4 answers)
Closed 3 years ago.
I want to build a regex where it searches for a string containing 12 digits in a row. If there's no match, look for a string with only 10 digits in a row.
For example:
a123456789012a
a1234567890a
Would return:
123456789012
And if the input is:
a1234a
a1234567890a
It would return:
1234567890
I managed to create the regex for the individual operations, beeing (?<!\d)\d{10}(?!\d) for 10 digits and (?<!\d)\d{12}(?!\d) for 12 digits, but I can't group them up in a if-else style.
I tried the following:
(?(?<!\d)\d{12}(?!\d)|((?<!\d)\d{10}(?!\d)))
but if the first pattern don't match, the regex don't try to match the second, returning nothing
You can use a simple regex like this:
\d{12}|\d{10}
working demo
Look that I have not used multiline nor global flags. This way the pattern is going to find the first match you want.
Case 1:
Case 2:
BTW, use capturing groups if you want to capture the content:
(\d{12}|\d{10})

Regex to validate letters numbers and specific special characters [duplicate]

This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.