This question already has answers here:
Reference - What does this regex mean?
(1 answer)
How can I match "anything up until this sequence of characters" in a regular expression?
(15 answers)
Greedy vs. Reluctant vs. Possessive Qualifiers
(7 answers)
In a regex, what changes when you add a question mark to .+
(2 answers)
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 5 years ago.
What is the difference between the below two expressions?
1. (\S+)
2. (\S+?)
I would appreciate if someone could explain this.
Thanks,
The first means match a single character that is a non-whitespace character, between one and unlimited times, as many times as possible, giving back as needed (greedy).
The second means Match a single character that is a non-whitespace character, between one and unlimited times, as few times as possible, expanding as needed (lazy).
The difference is greedy or lazy repetition. From the Regex Buddy help file:
A greedy quantifier will first try to repeat the token as many times as possible, and gradually give up matches as the engine backtracks to find an overall match. A lazy quantifier will first repeat the token as few times as required, and gradually expand the match as the engine backtracks through the regex to find an overall match.
The differences can be seen in the images below:
Related
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.
This question already has answers here:
Regular expression to match a line that doesn't contain a word
(34 answers)
Closed 5 years ago.
I've tried to make a regular expression that match anything except if contains a 11 digits like 12345678910 so don't match anything
what i have tried
[^\d{11}]
but {11} doesn't work with \d expression
so what i have to do ?
you can use the regex
^(?!.*\d{11}).*$
see the regex101 demo
It's not a very good task for regex to solve actually, because you have to describe every string that doesn't contain 11 consecutive digits.
If possible, I suggest matching a string that does contain 11 consecutive digits, then inverting the success of that match with the language or tool from which you execute this regex.
Depending on your regex flavour, you might also be able to use a negative lookahead such as presented in other answers.
This seemed to work for me using a negative look around:
/^((?!\d{11}).)*$/gm
This question already has answers here:
What is a non-capturing group in regular expressions?
(18 answers)
Closed 6 years ago.
This regex is supposed to match any numbers (real or integer - no scientific notation). However, I am not sure what is the use of '?:' inside the parentheses.
Could anyone explain this along with some examples? Thank you very much.
In the regex
?\d+(?:\.\d+)?
The ?: quantity inside the group in parenthesis instructs the regex engine to not capture the group, which it otherwise would.
By not capturing the quantity in parenthesis, the capture group available (which should be the first one, and the entire expression) would just be the digits occurring before the decimal point, should the number have a fractional component.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 8 years ago.
What does "\S" equal in Regex?
I have a regex:
/<((?:https?\:\/\/)*(?:[^\/?#])\/*\S*)>/ig;
trying to match:
What does \S equal? e.g.: [\w\d?:"-_]
\S matches anything except whitespace.
Regard as the opposite of \s (which matches whitespace).
(Personally I find \S obfuscating for this reason, particularly when viewing in some fonts where S and s look too similar. I prefer [^\s]).
\S means "Non-whitespace characters".
See Wikipedia
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