Regex to find the first capital letter in a string [duplicate] - regex

This question already has answers here:
How to make regex match only first occurrence of each match?
(4 answers)
Closed last year.
I have looked all over for an answer to this, but no one has had quite the same question as I do.
The problem I am trying to solve is to find the first capital letter in a string and no other capital letters. I have been able to narrow my search to each instance of a capital letter, but no amount of quantifiers I could find can make it only the first one.
For example, in this String abcDefgH. I only want to match the D and not the H.

Use a simple
([A-Z])I maybe missunderstood your question, but this will select only the first Capitalized letter in string.

Related

What went wrong with a regular expression? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 3 years ago.
I have a specific regular expression, \Good .+\.\. To my understanding that means, match each pattern that starts with "Good ", then any number of word characters (one or more) and finally end with a dot ('.').
So "Good morning." could is a pattern that this regex is matching, also "Good afternoon.", "Good day.", etc. But somehow it also matches the pattern "Good morning. Good afternoon. Good day." as a whole.
How is this possible?
As #Nick noted, .+ absorbs the final \.. I believe it's an example of a greedy expression where an expression tries to match the longest possible string.

Regex with exactly one space and special characters [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
Unfortunately I found that the existing examples are confusing and not similar enough to what I am trying to achieve. I need a regular expression to find occurencies of strings like
=> Test[a]
where between the special character > and Test there is exactly one space. The word Test can be replaced by any alphabetic string (=> Apple[b] is another example). I have worked out a regex for all except the first part with the block =>.
Can anyone help me?
I managed to find this expression
.(=>) [a-zA-Z]+\[a\]\.
and it works! Thanks everyone.
Try use this regex:
=> \S+
Here Is Demo

How to forbid symbol in the begin and end of group of letters and numbers [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 5 years ago.
Here is my regex that tries to match a valid URL:
^(https?:\/\/((\b\w[^-][a-zA-Z0-9-]{1,33})\.){1,34}([[a-zA-Z0-9]{1,6})\/?)$
and I've tried to find way to make a simple solution to forbid the use of a hyphen '-' in the beginning and end of a group of letters and numbers.
I'd tried to use \b\w[^-]. But it hasn't helped.
For example, my regex matches this string, but it shouldn't
http://example-.com
I found an answer by myself
^(https?:\/\/([WWW\.]|[www\.])?((([a-zA-Z0-9]|[a-zA-Z0-9][-][a-zA-Z0-9]){1,10})\.){1,33}([[a-zA-Z0-9]{1,6})\/?)$
its just a simple OR [a-zA-Z0-9]|[a-zA-Z0-9][-][a-zA-Z0-9]
If you'll find better solution pls send it :)

Matching first occurrence? [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regular expression to stop at first match
(9 answers)
Closed 3 years ago.
I know this question has been asked many times, but when I attempted to use the accepted answer that I found here, it does not work, so I assume I'm missing something.
I was attempting to match the Mrs. in the string Rothschild, Mrs. Martin (Elizabeth L. Barrett) using this regular express:
.*, (.*\.).*
But this does not work because of the L.. I then attempted to add the ? a number of different ways, but it still matches all the way to L.. Some things I tried:
.*, (.*\.?).*
.*, (.*\.*?).*
.*, (.*\.+?).*
.*, (.*\.??).*
But none of these work. Can anyone see what I am missing here?
Regex Fiddle
Put ? after the * which was present inside the capturing group. .* is greedy and eats up characters as many as possible. You need to add a quantifier ? after the * to do a shortest possible match.
.*, (.*?\.).*
DEMO

Regex expression for password validation [duplicate]

This question already has an answer here:
Regex to validate password
(1 answer)
Closed 9 years ago.
I'm trying to develop a regular expression for validating a password which should meet following criteria
should have at least one Uppercase letter,
should have at least one Lowercase letter,
should have at least one Special character,
should have at least one digit,
must be minimum 6 characters long.
I have developed a expression for that:
password_pattern=/^(?=.*[0-9]) (?=.*[!##$%^&*]) (?=.*[a-z]) (?=.*[A-Z]) {6} $/
However it is not working as I intended. What am I going wrong?
I'm new to regular expressions, so I'd appreciate an explanation rather than a 'use this' sort of answer, please explain.
You are missing a single dot before the {6}, and you've added spaces, which you shouldn't have:
password_pattern=/^(?=.*[0-9])(?=.*[!##$%^&*])(?=.*[a-z])(?=.*[A-Z]).{6}$/