Regex is too greedy. Cannot find a way to fix it

Multi tool use
Is there a way to fix the following regex? I have included an example in regex101. Basically it captures too much and a wrong part between ()
tags. It kind of does what it's supposed to but in turn I lose text and another tag.
https://regex101.com/r/OPRCuh/1
regex:
[(.+?)]((https.+?))
sample text
_“[Developer Interview](/blog/tags/developer_interview.html)” is a new series here at Semaphore blog. We’ll interview developers from some of the companies using [text text text](https://textapp.com) to find out how they work and share their insights with you.
regex
add a comment |
Is there a way to fix the following regex? I have included an example in regex101. Basically it captures too much and a wrong part between ()
tags. It kind of does what it's supposed to but in turn I lose text and another tag.
https://regex101.com/r/OPRCuh/1
regex:
[(.+?)]((https.+?))
sample text
_“[Developer Interview](/blog/tags/developer_interview.html)” is a new series here at Semaphore blog. We’ll interview developers from some of the companies using [text text text](https://textapp.com) to find out how they work and share their insights with you.
regex
Use.*?
instead of.+?
– emix
Dec 30 '18 at 20:58
add a comment |
Is there a way to fix the following regex? I have included an example in regex101. Basically it captures too much and a wrong part between ()
tags. It kind of does what it's supposed to but in turn I lose text and another tag.
https://regex101.com/r/OPRCuh/1
regex:
[(.+?)]((https.+?))
sample text
_“[Developer Interview](/blog/tags/developer_interview.html)” is a new series here at Semaphore blog. We’ll interview developers from some of the companies using [text text text](https://textapp.com) to find out how they work and share their insights with you.
regex
Is there a way to fix the following regex? I have included an example in regex101. Basically it captures too much and a wrong part between ()
tags. It kind of does what it's supposed to but in turn I lose text and another tag.
https://regex101.com/r/OPRCuh/1
regex:
[(.+?)]((https.+?))
sample text
_“[Developer Interview](/blog/tags/developer_interview.html)” is a new series here at Semaphore blog. We’ll interview developers from some of the companies using [text text text](https://textapp.com) to find out how they work and share their insights with you.
regex
regex
edited Dec 30 '18 at 20:57


jonrsharpe
77.5k11104211
77.5k11104211
asked Dec 30 '18 at 20:55
gointerngointern
135
135
Use.*?
instead of.+?
– emix
Dec 30 '18 at 20:58
add a comment |
Use.*?
instead of.+?
– emix
Dec 30 '18 at 20:58
Use
.*?
instead of .+?
– emix
Dec 30 '18 at 20:58
Use
.*?
instead of .+?
– emix
Dec 30 '18 at 20:58
add a comment |
1 Answer
1
active
oldest
votes
The .
pattern matches any char other than a line break char. So, it can match [
, ]
, (
and )
, too, until it finds a valid match. Since the regex parses the string from left to right, the regex engine finds the first [
and then finds ]
after Interview
, then finds (
before /blog
but gives it up since it is not followed with https
, but still goes on to match chars until it finds (https
and thus returns a valid match.
You may use
r'[([^]*)]((https[^()]*))'
See the regex demo
The [^]*
pattern matches 0+ chars other than [
and ]
and [^()]*
matches 0+ chars other than (
and )
.
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
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%2f53981356%2fregex-is-too-greedy-cannot-find-a-way-to-fix-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The .
pattern matches any char other than a line break char. So, it can match [
, ]
, (
and )
, too, until it finds a valid match. Since the regex parses the string from left to right, the regex engine finds the first [
and then finds ]
after Interview
, then finds (
before /blog
but gives it up since it is not followed with https
, but still goes on to match chars until it finds (https
and thus returns a valid match.
You may use
r'[([^]*)]((https[^()]*))'
See the regex demo
The [^]*
pattern matches 0+ chars other than [
and ]
and [^()]*
matches 0+ chars other than (
and )
.
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
add a comment |
The .
pattern matches any char other than a line break char. So, it can match [
, ]
, (
and )
, too, until it finds a valid match. Since the regex parses the string from left to right, the regex engine finds the first [
and then finds ]
after Interview
, then finds (
before /blog
but gives it up since it is not followed with https
, but still goes on to match chars until it finds (https
and thus returns a valid match.
You may use
r'[([^]*)]((https[^()]*))'
See the regex demo
The [^]*
pattern matches 0+ chars other than [
and ]
and [^()]*
matches 0+ chars other than (
and )
.
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
add a comment |
The .
pattern matches any char other than a line break char. So, it can match [
, ]
, (
and )
, too, until it finds a valid match. Since the regex parses the string from left to right, the regex engine finds the first [
and then finds ]
after Interview
, then finds (
before /blog
but gives it up since it is not followed with https
, but still goes on to match chars until it finds (https
and thus returns a valid match.
You may use
r'[([^]*)]((https[^()]*))'
See the regex demo
The [^]*
pattern matches 0+ chars other than [
and ]
and [^()]*
matches 0+ chars other than (
and )
.
The .
pattern matches any char other than a line break char. So, it can match [
, ]
, (
and )
, too, until it finds a valid match. Since the regex parses the string from left to right, the regex engine finds the first [
and then finds ]
after Interview
, then finds (
before /blog
but gives it up since it is not followed with https
, but still goes on to match chars until it finds (https
and thus returns a valid match.
You may use
r'[([^]*)]((https[^()]*))'
See the regex demo
The [^]*
pattern matches 0+ chars other than [
and ]
and [^()]*
matches 0+ chars other than (
and )
.
answered Dec 30 '18 at 20:58
Wiktor StribiżewWiktor Stribiżew
313k16133210
313k16133210
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
add a comment |
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
Excellent. Thank you very much!
– gointern
Dec 30 '18 at 21:32
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
@gointern Glad it worked for you. Please also consider upvoting if my answer proved helpful to you (see How to upvote on Stack Overflow?) as you are entitled to the upvoting privilege after reaching 15 rep points. Note you may upvote all the answers that turned out helpful.
– Wiktor Stribiżew
Dec 30 '18 at 21:52
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.
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%2f53981356%2fregex-is-too-greedy-cannot-find-a-way-to-fix-it%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
p9d9fKYCysF,irsDOeXVrW7QTruC3,o,L3i,D,PZ7JBemyu45
Use
.*?
instead of.+?
– emix
Dec 30 '18 at 20:58