What is the meaning of + in a regex?
What does the plus symbol in regex mean?
regex symbol
add a comment |
What does the plus symbol in regex mean?
regex symbol
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
10
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
1
^^ matchessample
,samplee
, andsssaaaaaammmppplllllle
but notsmple
– NoodleOfDeath
Aug 14 '16 at 16:59
add a comment |
What does the plus symbol in regex mean?
regex symbol
What does the plus symbol in regex mean?
regex symbol
regex symbol
edited Nov 24 '13 at 1:45
PeeHaa
49.7k41165241
49.7k41165241
asked Oct 3 '10 at 14:52
NoodleOfDeathNoodleOfDeath
5,370175579
5,370175579
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
10
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
1
^^ matchessample
,samplee
, andsssaaaaaammmppplllllle
but notsmple
– NoodleOfDeath
Aug 14 '16 at 16:59
add a comment |
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
10
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
1
^^ matchessample
,samplee
, andsssaaaaaammmppplllllle
but notsmple
– NoodleOfDeath
Aug 14 '16 at 16:59
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
10
10
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
1
1
^^ matches
sample
, samplee
, and sssaaaaaammmppplllllle
but not smple
– NoodleOfDeath
Aug 14 '16 at 16:59
^^ matches
sample
, samplee
, and sssaaaaaammmppplllllle
but not smple
– NoodleOfDeath
Aug 14 '16 at 16:59
add a comment |
4 Answers
4
active
oldest
votes
+
can actually have two meanings, depending on context.
Like the other answers mentioned, +
usually is a repetition operator, and causes the preceding token to repeat one or more times. a+
would be expressed as aa*
in formal language theory, and could also be expressed as a{1,}
(match a minimum of 1 times and a maximum of infinite times).
However, +
can also make other quantifiers possessive if it follows a repetition operator (ie ?+
, *+
, ++
or {m,n}+
). A possessive quantifier is an advanced feature of some regex flavours (PCRE, Java and the JGsoft engine) which tells the engine not to backtrack once a match has been made.
To understand how this works, we need to understand two concepts of regex engines: greediness and backtracking. Greediness means that in general regexes will try to consume as many characters as they can. Let's say our pattern is .*
(the dot is a special construct in regexes which means any character1; the star means match zero or more times), and your target is aaaaaaaab
. The entire string will be consumed, because the entire string is the longest match that satisfies the pattern.
However, let's say we change the pattern to .*b
. Now, when the regex engine tries to match against aaaaaaaab
, the .*
will again consume the entire string. However, since the engine will have reached the end of the string and the pattern is not yet satisfied (the .*
consumed everything but the pattern still has to match b
afterwards), it will backtrack, one character at a time, and try to match b
. The first backtrack will make the .*
consume aaaaaaaa
, and then b
can consume b
, and the pattern succeeds.
Possessive quantifiers are also greedy, but as mentioned, once they return a match, the engine can no longer backtrack past that point. So if we change our pattern to .*+b
(match any character zero or more times, possessively, followed by a b
), and try to match aaaaaaaab
, again the .*
will consume the whole string, but then since it is possessive, backtracking information is discarded, and the b cannot be matched so the pattern fails.
1 In most engines, the dot will not match a newline character, unless the /s
("singleline" or "dotall") modifier is specified.
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups(?>.*)
.
– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
add a comment |
In most implementations +
means "one or more".
In some theoretical writings +
is used to mean "or" (most implementations use the |
symbol for that).
add a comment |
1 or more of previous expression.
[0-9]+
Would match:
1234567890
In:
I have 1234567890 dollars
add a comment |
One or more occurences of the preceding symbols.
E.g. a+
means the letter a
one or more times. Thus, a
matches a
, aa
, aaaaaa
but not an empty string.
If you know what the asterisk (*
) means, then you can express (exp)+
as (exp)(exp)*
, where (exp)
is any regular expression.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3850217%2fwhat-is-the-meaning-of-in-a-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
+
can actually have two meanings, depending on context.
Like the other answers mentioned, +
usually is a repetition operator, and causes the preceding token to repeat one or more times. a+
would be expressed as aa*
in formal language theory, and could also be expressed as a{1,}
(match a minimum of 1 times and a maximum of infinite times).
However, +
can also make other quantifiers possessive if it follows a repetition operator (ie ?+
, *+
, ++
or {m,n}+
). A possessive quantifier is an advanced feature of some regex flavours (PCRE, Java and the JGsoft engine) which tells the engine not to backtrack once a match has been made.
To understand how this works, we need to understand two concepts of regex engines: greediness and backtracking. Greediness means that in general regexes will try to consume as many characters as they can. Let's say our pattern is .*
(the dot is a special construct in regexes which means any character1; the star means match zero or more times), and your target is aaaaaaaab
. The entire string will be consumed, because the entire string is the longest match that satisfies the pattern.
However, let's say we change the pattern to .*b
. Now, when the regex engine tries to match against aaaaaaaab
, the .*
will again consume the entire string. However, since the engine will have reached the end of the string and the pattern is not yet satisfied (the .*
consumed everything but the pattern still has to match b
afterwards), it will backtrack, one character at a time, and try to match b
. The first backtrack will make the .*
consume aaaaaaaa
, and then b
can consume b
, and the pattern succeeds.
Possessive quantifiers are also greedy, but as mentioned, once they return a match, the engine can no longer backtrack past that point. So if we change our pattern to .*+b
(match any character zero or more times, possessively, followed by a b
), and try to match aaaaaaaab
, again the .*
will consume the whole string, but then since it is possessive, backtracking information is discarded, and the b cannot be matched so the pattern fails.
1 In most engines, the dot will not match a newline character, unless the /s
("singleline" or "dotall") modifier is specified.
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups(?>.*)
.
– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
add a comment |
+
can actually have two meanings, depending on context.
Like the other answers mentioned, +
usually is a repetition operator, and causes the preceding token to repeat one or more times. a+
would be expressed as aa*
in formal language theory, and could also be expressed as a{1,}
(match a minimum of 1 times and a maximum of infinite times).
However, +
can also make other quantifiers possessive if it follows a repetition operator (ie ?+
, *+
, ++
or {m,n}+
). A possessive quantifier is an advanced feature of some regex flavours (PCRE, Java and the JGsoft engine) which tells the engine not to backtrack once a match has been made.
To understand how this works, we need to understand two concepts of regex engines: greediness and backtracking. Greediness means that in general regexes will try to consume as many characters as they can. Let's say our pattern is .*
(the dot is a special construct in regexes which means any character1; the star means match zero or more times), and your target is aaaaaaaab
. The entire string will be consumed, because the entire string is the longest match that satisfies the pattern.
However, let's say we change the pattern to .*b
. Now, when the regex engine tries to match against aaaaaaaab
, the .*
will again consume the entire string. However, since the engine will have reached the end of the string and the pattern is not yet satisfied (the .*
consumed everything but the pattern still has to match b
afterwards), it will backtrack, one character at a time, and try to match b
. The first backtrack will make the .*
consume aaaaaaaa
, and then b
can consume b
, and the pattern succeeds.
Possessive quantifiers are also greedy, but as mentioned, once they return a match, the engine can no longer backtrack past that point. So if we change our pattern to .*+b
(match any character zero or more times, possessively, followed by a b
), and try to match aaaaaaaab
, again the .*
will consume the whole string, but then since it is possessive, backtracking information is discarded, and the b cannot be matched so the pattern fails.
1 In most engines, the dot will not match a newline character, unless the /s
("singleline" or "dotall") modifier is specified.
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups(?>.*)
.
– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
add a comment |
+
can actually have two meanings, depending on context.
Like the other answers mentioned, +
usually is a repetition operator, and causes the preceding token to repeat one or more times. a+
would be expressed as aa*
in formal language theory, and could also be expressed as a{1,}
(match a minimum of 1 times and a maximum of infinite times).
However, +
can also make other quantifiers possessive if it follows a repetition operator (ie ?+
, *+
, ++
or {m,n}+
). A possessive quantifier is an advanced feature of some regex flavours (PCRE, Java and the JGsoft engine) which tells the engine not to backtrack once a match has been made.
To understand how this works, we need to understand two concepts of regex engines: greediness and backtracking. Greediness means that in general regexes will try to consume as many characters as they can. Let's say our pattern is .*
(the dot is a special construct in regexes which means any character1; the star means match zero or more times), and your target is aaaaaaaab
. The entire string will be consumed, because the entire string is the longest match that satisfies the pattern.
However, let's say we change the pattern to .*b
. Now, when the regex engine tries to match against aaaaaaaab
, the .*
will again consume the entire string. However, since the engine will have reached the end of the string and the pattern is not yet satisfied (the .*
consumed everything but the pattern still has to match b
afterwards), it will backtrack, one character at a time, and try to match b
. The first backtrack will make the .*
consume aaaaaaaa
, and then b
can consume b
, and the pattern succeeds.
Possessive quantifiers are also greedy, but as mentioned, once they return a match, the engine can no longer backtrack past that point. So if we change our pattern to .*+b
(match any character zero or more times, possessively, followed by a b
), and try to match aaaaaaaab
, again the .*
will consume the whole string, but then since it is possessive, backtracking information is discarded, and the b cannot be matched so the pattern fails.
1 In most engines, the dot will not match a newline character, unless the /s
("singleline" or "dotall") modifier is specified.
+
can actually have two meanings, depending on context.
Like the other answers mentioned, +
usually is a repetition operator, and causes the preceding token to repeat one or more times. a+
would be expressed as aa*
in formal language theory, and could also be expressed as a{1,}
(match a minimum of 1 times and a maximum of infinite times).
However, +
can also make other quantifiers possessive if it follows a repetition operator (ie ?+
, *+
, ++
or {m,n}+
). A possessive quantifier is an advanced feature of some regex flavours (PCRE, Java and the JGsoft engine) which tells the engine not to backtrack once a match has been made.
To understand how this works, we need to understand two concepts of regex engines: greediness and backtracking. Greediness means that in general regexes will try to consume as many characters as they can. Let's say our pattern is .*
(the dot is a special construct in regexes which means any character1; the star means match zero or more times), and your target is aaaaaaaab
. The entire string will be consumed, because the entire string is the longest match that satisfies the pattern.
However, let's say we change the pattern to .*b
. Now, when the regex engine tries to match against aaaaaaaab
, the .*
will again consume the entire string. However, since the engine will have reached the end of the string and the pattern is not yet satisfied (the .*
consumed everything but the pattern still has to match b
afterwards), it will backtrack, one character at a time, and try to match b
. The first backtrack will make the .*
consume aaaaaaaa
, and then b
can consume b
, and the pattern succeeds.
Possessive quantifiers are also greedy, but as mentioned, once they return a match, the engine can no longer backtrack past that point. So if we change our pattern to .*+b
(match any character zero or more times, possessively, followed by a b
), and try to match aaaaaaaab
, again the .*
will consume the whole string, but then since it is possessive, backtracking information is discarded, and the b cannot be matched so the pattern fails.
1 In most engines, the dot will not match a newline character, unless the /s
("singleline" or "dotall") modifier is specified.
edited Oct 3 '10 at 16:02
answered Oct 3 '10 at 15:01
Daniel VandersluisDaniel Vandersluis
67.3k14143144
67.3k14143144
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups(?>.*)
.
– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
add a comment |
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups(?>.*)
.
– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups
(?>.*)
.– Tim Pietzcker
Oct 3 '10 at 15:09
+1; possessive quantifiers only work in Java, PCRE, or the JGSoft regex engine, though. Ruby, Perl, and .NET use atomic groups
(?>.*)
.– Tim Pietzcker
Oct 3 '10 at 15:09
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim I alluded to that, but I've now made it more explicit in my answer.
– Daniel Vandersluis
Oct 3 '10 at 15:18
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@Tim: Perl does support possesive quantifiers, probably since 5.10
– ninjalj
Oct 3 '10 at 16:21
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
@ninjalj: Thanks for the info. It appears that this comparison is not up to date anymore, then.
– Tim Pietzcker
Oct 3 '10 at 17:54
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
Awesome answer! Really helped me understand the + alot. Thank you.
– realnsleo
Sep 27 '17 at 11:08
add a comment |
In most implementations +
means "one or more".
In some theoretical writings +
is used to mean "or" (most implementations use the |
symbol for that).
add a comment |
In most implementations +
means "one or more".
In some theoretical writings +
is used to mean "or" (most implementations use the |
symbol for that).
add a comment |
In most implementations +
means "one or more".
In some theoretical writings +
is used to mean "or" (most implementations use the |
symbol for that).
In most implementations +
means "one or more".
In some theoretical writings +
is used to mean "or" (most implementations use the |
symbol for that).
answered Oct 3 '10 at 14:54
sepp2ksepp2k
293k38593609
293k38593609
add a comment |
add a comment |
1 or more of previous expression.
[0-9]+
Would match:
1234567890
In:
I have 1234567890 dollars
add a comment |
1 or more of previous expression.
[0-9]+
Would match:
1234567890
In:
I have 1234567890 dollars
add a comment |
1 or more of previous expression.
[0-9]+
Would match:
1234567890
In:
I have 1234567890 dollars
1 or more of previous expression.
[0-9]+
Would match:
1234567890
In:
I have 1234567890 dollars
answered Oct 3 '10 at 14:58
ChrisChris
31517
31517
add a comment |
add a comment |
One or more occurences of the preceding symbols.
E.g. a+
means the letter a
one or more times. Thus, a
matches a
, aa
, aaaaaa
but not an empty string.
If you know what the asterisk (*
) means, then you can express (exp)+
as (exp)(exp)*
, where (exp)
is any regular expression.
add a comment |
One or more occurences of the preceding symbols.
E.g. a+
means the letter a
one or more times. Thus, a
matches a
, aa
, aaaaaa
but not an empty string.
If you know what the asterisk (*
) means, then you can express (exp)+
as (exp)(exp)*
, where (exp)
is any regular expression.
add a comment |
One or more occurences of the preceding symbols.
E.g. a+
means the letter a
one or more times. Thus, a
matches a
, aa
, aaaaaa
but not an empty string.
If you know what the asterisk (*
) means, then you can express (exp)+
as (exp)(exp)*
, where (exp)
is any regular expression.
One or more occurences of the preceding symbols.
E.g. a+
means the letter a
one or more times. Thus, a
matches a
, aa
, aaaaaa
but not an empty string.
If you know what the asterisk (*
) means, then you can express (exp)+
as (exp)(exp)*
, where (exp)
is any regular expression.
answered Oct 3 '10 at 14:54
phimuemuephimuemue
20.2k56098
20.2k56098
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3850217%2fwhat-is-the-meaning-of-in-a-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you please post a sample?
– kennytm
Oct 3 '10 at 15:00
10
/s+a+m+p+l+e+/
– Ring Ø
Oct 3 '10 at 15:28
1
^^ matches
sample
,samplee
, andsssaaaaaammmppplllllle
but notsmple
– NoodleOfDeath
Aug 14 '16 at 16:59