How to replace stuff between two flag symbols using regex












-1















For example I have below text




some {}} outside, and some inside $aa{bb}cc{{dd}ee}ff$, and some more $aa{bb}cc{{dd}ee}ff$ here.




I call $ flag symbol, and now I want to replace all { or } between pairs of $...$ using regex.



The only way I can think of is to replace multiple times, using regex



($.*?)({|})


each time it replaces a { or } inside $...$. After enough time of replacement, we got




some {}} outside, and some inside $aabbccddeeff$, and some more
$aabbccddeeff$ here.




The drawback is that you don't know how many times it needs to get { or } completely replaced, and it apparently not efficient at all.



So I am wondering, is regex capable to doing this in a single run? For regex flavor I mean perl compatible. But if perl can not do this, I would love to know if other regex flavor can do.










share|improve this question




















  • 1





    What language are you implementing this in? Can you post an example expected output for your input there?

    – CertainPerformance
    Jan 1 at 4:27













  • Hi ,@CertainPerformance I updated my post

    – user15964
    Jan 1 at 4:33











  • regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

    – Charles Duffy
    Jan 1 at 4:41


















-1















For example I have below text




some {}} outside, and some inside $aa{bb}cc{{dd}ee}ff$, and some more $aa{bb}cc{{dd}ee}ff$ here.




I call $ flag symbol, and now I want to replace all { or } between pairs of $...$ using regex.



The only way I can think of is to replace multiple times, using regex



($.*?)({|})


each time it replaces a { or } inside $...$. After enough time of replacement, we got




some {}} outside, and some inside $aabbccddeeff$, and some more
$aabbccddeeff$ here.




The drawback is that you don't know how many times it needs to get { or } completely replaced, and it apparently not efficient at all.



So I am wondering, is regex capable to doing this in a single run? For regex flavor I mean perl compatible. But if perl can not do this, I would love to know if other regex flavor can do.










share|improve this question




















  • 1





    What language are you implementing this in? Can you post an example expected output for your input there?

    – CertainPerformance
    Jan 1 at 4:27













  • Hi ,@CertainPerformance I updated my post

    – user15964
    Jan 1 at 4:33











  • regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

    – Charles Duffy
    Jan 1 at 4:41
















-1












-1








-1








For example I have below text




some {}} outside, and some inside $aa{bb}cc{{dd}ee}ff$, and some more $aa{bb}cc{{dd}ee}ff$ here.




I call $ flag symbol, and now I want to replace all { or } between pairs of $...$ using regex.



The only way I can think of is to replace multiple times, using regex



($.*?)({|})


each time it replaces a { or } inside $...$. After enough time of replacement, we got




some {}} outside, and some inside $aabbccddeeff$, and some more
$aabbccddeeff$ here.




The drawback is that you don't know how many times it needs to get { or } completely replaced, and it apparently not efficient at all.



So I am wondering, is regex capable to doing this in a single run? For regex flavor I mean perl compatible. But if perl can not do this, I would love to know if other regex flavor can do.










share|improve this question
















For example I have below text




some {}} outside, and some inside $aa{bb}cc{{dd}ee}ff$, and some more $aa{bb}cc{{dd}ee}ff$ here.




I call $ flag symbol, and now I want to replace all { or } between pairs of $...$ using regex.



The only way I can think of is to replace multiple times, using regex



($.*?)({|})


each time it replaces a { or } inside $...$. After enough time of replacement, we got




some {}} outside, and some inside $aabbccddeeff$, and some more
$aabbccddeeff$ here.




The drawback is that you don't know how many times it needs to get { or } completely replaced, and it apparently not efficient at all.



So I am wondering, is regex capable to doing this in a single run? For regex flavor I mean perl compatible. But if perl can not do this, I would love to know if other regex flavor can do.







regex pcre






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 4:45









Charles Duffy

178k25201256




178k25201256










asked Jan 1 at 4:26









user15964user15964

790824




790824








  • 1





    What language are you implementing this in? Can you post an example expected output for your input there?

    – CertainPerformance
    Jan 1 at 4:27













  • Hi ,@CertainPerformance I updated my post

    – user15964
    Jan 1 at 4:33











  • regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

    – Charles Duffy
    Jan 1 at 4:41
















  • 1





    What language are you implementing this in? Can you post an example expected output for your input there?

    – CertainPerformance
    Jan 1 at 4:27













  • Hi ,@CertainPerformance I updated my post

    – user15964
    Jan 1 at 4:33











  • regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

    – Charles Duffy
    Jan 1 at 4:41










1




1





What language are you implementing this in? Can you post an example expected output for your input there?

– CertainPerformance
Jan 1 at 4:27







What language are you implementing this in? Can you post an example expected output for your input there?

– CertainPerformance
Jan 1 at 4:27















Hi ,@CertainPerformance I updated my post

– user15964
Jan 1 at 4:33





Hi ,@CertainPerformance I updated my post

– user15964
Jan 1 at 4:33













regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

– Charles Duffy
Jan 1 at 4:41







regexes don't by the themselves support any kind of replacement, modification, etc -- matching is the only thing they do. So you need more tags for whatever tool or layer you're adding that supports making changes.

– Charles Duffy
Jan 1 at 4:41














1 Answer
1






active

oldest

votes


















1














Try this regex:



[{}](?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$)


Click for Demo



Replace each match with a blank string.



Explanation:





  • [{}] - matches either { or }


  • (?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$) - positive lookahead to make sure that the above match is followed by odd number of $ somewhere later in the string. This would make sure that { or } is followed by 1 or 3 or 5 or 7... instances of $ somewhere later in the string.






share|improve this answer





















  • 1





    Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

    – Charles Duffy
    Jan 1 at 4:42













  • OP has mentioned that Perl compatible regex is required.

    – Potato
    Jan 1 at 4:44











  • So they have; adding that to the question's tagging.

    – Charles Duffy
    Jan 1 at 4:45











  • Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

    – user15964
    Jan 1 at 6:51











  • Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

    – Potato
    Jan 1 at 7:53











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993000%2fhow-to-replace-stuff-between-two-flag-symbols-using-regex%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









1














Try this regex:



[{}](?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$)


Click for Demo



Replace each match with a blank string.



Explanation:





  • [{}] - matches either { or }


  • (?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$) - positive lookahead to make sure that the above match is followed by odd number of $ somewhere later in the string. This would make sure that { or } is followed by 1 or 3 or 5 or 7... instances of $ somewhere later in the string.






share|improve this answer





















  • 1





    Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

    – Charles Duffy
    Jan 1 at 4:42













  • OP has mentioned that Perl compatible regex is required.

    – Potato
    Jan 1 at 4:44











  • So they have; adding that to the question's tagging.

    – Charles Duffy
    Jan 1 at 4:45











  • Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

    – user15964
    Jan 1 at 6:51











  • Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

    – Potato
    Jan 1 at 7:53
















1














Try this regex:



[{}](?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$)


Click for Demo



Replace each match with a blank string.



Explanation:





  • [{}] - matches either { or }


  • (?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$) - positive lookahead to make sure that the above match is followed by odd number of $ somewhere later in the string. This would make sure that { or } is followed by 1 or 3 or 5 or 7... instances of $ somewhere later in the string.






share|improve this answer





















  • 1





    Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

    – Charles Duffy
    Jan 1 at 4:42













  • OP has mentioned that Perl compatible regex is required.

    – Potato
    Jan 1 at 4:44











  • So they have; adding that to the question's tagging.

    – Charles Duffy
    Jan 1 at 4:45











  • Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

    – user15964
    Jan 1 at 6:51











  • Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

    – Potato
    Jan 1 at 7:53














1












1








1







Try this regex:



[{}](?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$)


Click for Demo



Replace each match with a blank string.



Explanation:





  • [{}] - matches either { or }


  • (?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$) - positive lookahead to make sure that the above match is followed by odd number of $ somewhere later in the string. This would make sure that { or } is followed by 1 or 3 or 5 or 7... instances of $ somewhere later in the string.






share|improve this answer















Try this regex:



[{}](?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$)


Click for Demo



Replace each match with a blank string.



Explanation:





  • [{}] - matches either { or }


  • (?=[^$]*$(?:(?:[^$]*$){2})*[^$]*$) - positive lookahead to make sure that the above match is followed by odd number of $ somewhere later in the string. This would make sure that { or } is followed by 1 or 3 or 5 or 7... instances of $ somewhere later in the string.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 4:46

























answered Jan 1 at 4:40









PotatoPotato

7,67821133




7,67821133








  • 1





    Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

    – Charles Duffy
    Jan 1 at 4:42













  • OP has mentioned that Perl compatible regex is required.

    – Potato
    Jan 1 at 4:44











  • So they have; adding that to the question's tagging.

    – Charles Duffy
    Jan 1 at 4:45











  • Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

    – user15964
    Jan 1 at 6:51











  • Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

    – Potato
    Jan 1 at 7:53














  • 1





    Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

    – Charles Duffy
    Jan 1 at 4:42













  • OP has mentioned that Perl compatible regex is required.

    – Potato
    Jan 1 at 4:44











  • So they have; adding that to the question's tagging.

    – Charles Duffy
    Jan 1 at 4:45











  • Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

    – user15964
    Jan 1 at 6:51











  • Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

    – Potato
    Jan 1 at 7:53








1




1





Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

– Charles Duffy
Jan 1 at 4:42







Definitely not generic to all regex variants. Has the OP indicated that they have PCRE extensions available? Much of this ((?:...)) doesn't work even in POSIX ERE, much less BRE; and even some modern engines intentionally don't support lookahead/lookbehind (since those features have serious impact on worst-case performance).

– Charles Duffy
Jan 1 at 4:42















OP has mentioned that Perl compatible regex is required.

– Potato
Jan 1 at 4:44





OP has mentioned that Perl compatible regex is required.

– Potato
Jan 1 at 4:44













So they have; adding that to the question's tagging.

– Charles Duffy
Jan 1 at 4:45





So they have; adding that to the question's tagging.

– Charles Duffy
Jan 1 at 4:45













Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

– user15964
Jan 1 at 6:51





Thank you so much! Your trick is so clever. There remains one thing I am not sure. Why we have to use non capture group? I found simply use capture group like [{}](?=[^$]*$(([^$]*$){2})*[^$]*$) also give the same result. But the highlight is somewhat different on regex101.com. See regex101.com/r/nkgjPn/2

– user15964
Jan 1 at 6:51













Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

– Potato
Jan 1 at 7:53





Yes you can remove the non-capturing group. In that case, whatever is matched by the regex pattern inside the parenthesis, will be captured in a Group. Refer THIS to know more about Regex Groups.

– Potato
Jan 1 at 7:53




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993000%2fhow-to-replace-stuff-between-two-flag-symbols-using-regex%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith