Difference between .+? and .*? in regular expressions
. matches any single character except " ". To match any character including " ", use a pattern like "[sS]".
? | Matches the preceding subexpression 0 to 1 times |
---|---|
* | matches the preceding subexpression any number of times |
+ | matches the preceding subexpression one or more times >=1) |
? | Matches the preceding subexpression 0 to 1 times |
Add a question mark to indicate lazy mode
.+? means match one or more of any character
.*? means match 0 or more of any character
In specific use.+? may be easier to use, depending on personal preference
Now that it is written, let’s complete the regular knowledge by the way;;
" " : Does not consume any characters and only matches one position. It is often used to match word boundaries. For example, I want to match a single word "is" from a string "This is Regex". Regularly write "is" won't match the characters on either side of is, but it will recognize if both sides of is are word boundaries "d": matches digits, "w": matches letters, numbers, underscores. "s": matches spaces ".": matches anything but a newline () any character other than This is an enhanced version of "w". "w" cannot match spaces. If you use "w" to add spaces to the string, it will be limited. How to use "." to match the character "a23 4 5 BC D__TTz" Regular: ".+" "[abc]": The character group matches the character containing the element inside the brackets
matching ability ( . > w > d )
Antonym : The
writing method is very simple and can be changed to uppercase , which is also easy to understand;
"W" matches any character that is not a letter, number, or underscore "S" matches any character that is not whitespace "D" matches any non-digit character "B" matches a position that is not the beginning or end of a word "[^abc]" matches any character except abc"{n}" repeats n times
For example, from "aaaaaaaa" to match the a of the string and repeat it 3 times: "a{3}" The result is to get 3 a characters "aaa";
"{n,m}" repeats n to m times
For example, the regular "a{3,4}" will match a repeatedly 3 or 4 times, so the characters for matching can be three "aaa" or four "aaaa", all regulars can match
"{n,}" repeats n or more times
The difference from {n,m} is that there will be no upper limit on the number of matches, but it must be repeated at least n times such as regular "a{3,}" a must be repeated at least 3 times
"??" repeats 0 or 1 times, but as few as possible
For example "aaacb" regular "a.??b" will only get the last three characters "acb"
"{n,m}?" Repeat n to m times, but as little as possible
Such as "aaaaaa" regular "a{0,m}" because at least 0 times, the result is empty
"{n,}?" Repeat more than n times, but as few as possible
Such as "aaaaa" regular "a{1,}" is at least 1 time, so the result is "a"
n+ | matches n at least once (>=1) |
---|---|
n? | Match n 0 times or once (with and without) |
n* | Match n random times (* can represent any letter) |
?=n | matches any string immediately followed by the specified string n |
?!n | Matches any string not immediately followed by the specified string n. |
==> Both are equivalent to each other
n? | n{0,1} |
---|---|
n+ | n{1,} |
n* | n{0,} |
So far, this article about the difference between .+? and .*? in regular expressions is introduced. For more related regular expressions .+? and .*?
0 Comments