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

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

Related

Regular expression: Should not start with 5 digits (or more) in a row [duplicate]

This question already has an answer here:
Regular expression for a string that does not start with a sequence
(1 answer)
Closed 1 year ago.
I need to generate a regular expression to validate that the string does not start with 5 digits.
NOT VALID: 12345testing123asd
VALID: 1234testing1234
testing12345
testing
I tried to get the first five chars ^.{0,5} but I do not know hot to add the restriction of \D to those first 5 chars
Also, I tried with this [^0-9][^0-9][^0-9][^0-9][^0-9] but I do not know how to do to include the strings that starts with 4 or less numbers
Could you please help me with this? I am a rookie :(
If your RegExp flavor of choice supports negative lookaheads, this pattern will match if the string is valid (does not start with 5 or more consecutive digits):
^(?!\d{5,})
Regex101
Matches:
1234testing1234
testing12345
testing
Does not match:
12345testing123asd

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

How regex match exact 3 numbers [duplicate]

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

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

Perl regex to match phone numbers [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 7 years ago.
Regex:
\b(\(\d{3}\)|\d{3})?[-.]?\d{3}[-]?\d{4}\b
My input file has two types of phone numbers. One, whose first 3 digits are enclosed in parenthesis and the other with no parenthesis.
Eg:
"(201)-450-4479" ,"234-345-3456"
I want to match both type of phone numbers using alternate operator.
Please suggest me. What modification is required for above mentioned expression to get the intended result?
\b matches at a word-nonword boundary. If such a boundary should appear before (, it must be preceded by a word character, not whitespace or nothing.
Cf.
print /\b\(/ ? 1 : 0 for '(', ' (', 'a(';
Remove the starting \b from the regex, or replace it with
(?x: \b | \s | ^ )
I'd use this:
(\(?\d+\)?\-\d+\-\d+)
or using the alternate operator:
(\d+\-\d+\-\d+|\(\d+\)\-\d+\-\d+)