Regex for value inside mustache braces while ignoring erroneous whitespace












0














I am currently attempting to use Regex to obtain the value inside Mustache syntax braces e.g



Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun fact }}



I am trying to select everything except the braces and the leading and trailing white space (In the case the user adds more accidentally) or rather i would like to use groups to capture these 3 elements




  • group 1: '{' and leading whitespace

  • group 2: value we want

  • group 3: trailing white space and '}'


so far i have come up with the following ({{2,3}s*)([^{}]*)(s*}{2,3}) but if you check the capture groups the 2nd group matches then value we want...Which is good, but also the trailing white space which should be in the third group. i.e




  • group 1: '{' and leading whitespace

  • group 2: value we want with trailing white space

  • group 3: '}'


Ruby Implementation
str.gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }










share|improve this question




















  • 2




    Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
    – anothermh
    Nov 20 at 3:51
















0














I am currently attempting to use Regex to obtain the value inside Mustache syntax braces e.g



Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun fact }}



I am trying to select everything except the braces and the leading and trailing white space (In the case the user adds more accidentally) or rather i would like to use groups to capture these 3 elements




  • group 1: '{' and leading whitespace

  • group 2: value we want

  • group 3: trailing white space and '}'


so far i have come up with the following ({{2,3}s*)([^{}]*)(s*}{2,3}) but if you check the capture groups the 2nd group matches then value we want...Which is good, but also the trailing white space which should be in the third group. i.e




  • group 1: '{' and leading whitespace

  • group 2: value we want with trailing white space

  • group 3: '}'


Ruby Implementation
str.gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }










share|improve this question




















  • 2




    Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
    – anothermh
    Nov 20 at 3:51














0












0








0







I am currently attempting to use Regex to obtain the value inside Mustache syntax braces e.g



Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun fact }}



I am trying to select everything except the braces and the leading and trailing white space (In the case the user adds more accidentally) or rather i would like to use groups to capture these 3 elements




  • group 1: '{' and leading whitespace

  • group 2: value we want

  • group 3: trailing white space and '}'


so far i have come up with the following ({{2,3}s*)([^{}]*)(s*}{2,3}) but if you check the capture groups the 2nd group matches then value we want...Which is good, but also the trailing white space which should be in the third group. i.e




  • group 1: '{' and leading whitespace

  • group 2: value we want with trailing white space

  • group 3: '}'


Ruby Implementation
str.gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }










share|improve this question















I am currently attempting to use Regex to obtain the value inside Mustache syntax braces e.g



Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun fact }}



I am trying to select everything except the braces and the leading and trailing white space (In the case the user adds more accidentally) or rather i would like to use groups to capture these 3 elements




  • group 1: '{' and leading whitespace

  • group 2: value we want

  • group 3: trailing white space and '}'


so far i have come up with the following ({{2,3}s*)([^{}]*)(s*}{2,3}) but if you check the capture groups the 2nd group matches then value we want...Which is good, but also the trailing white space which should be in the third group. i.e




  • group 1: '{' and leading whitespace

  • group 2: value we want with trailing white space

  • group 3: '}'


Ruby Implementation
str.gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }







ruby regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 4:14

























asked Nov 20 at 1:53









Genhain

1,14411336




1,14411336








  • 2




    Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
    – anothermh
    Nov 20 at 3:51














  • 2




    Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
    – anothermh
    Nov 20 at 3:51








2




2




Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
– anothermh
Nov 20 at 3:51




Do not include links to external websites when you can easily copy and paste the content from those sites into your question. When that link stops working no one will be able to see the work you’ve done. It’s 32 characters in a single line.
– anothermh
Nov 20 at 3:51












1 Answer
1






active

oldest

votes


















2














One option is to scan your input string into an array matches, using the following pattern:



{+s*([^{}]+)s*}+


This would capture all content appearing in between (presumably) nested sets of curly braces. Then, we can flatten that array into a single level array of string matches, from the single capture group, and collect, removing leading and trailing whitespace.



str = "Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun  fact    }}"
arr = str.scan(/{+s*([^{}]+)s*}+/)
print arr.flatten.collect{|x| x.strip || x }

["name", "greeting", "code question website!!! ?? ?", "random fun fact"]





share|improve this answer





















  • That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
    – Genhain
    Nov 20 at 3:38






  • 2




    Well that isn't what your question says, and you should not change your question after others have already answered.
    – Tim Biegeleisen
    Nov 20 at 3:42






  • 2




    @Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
    – anothermh
    Nov 20 at 3:57










  • @TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
    – Genhain
    Nov 20 at 4:10










  • Ask a new question and reference this one, and maybe use my pattern if that would help at all.
    – Tim Biegeleisen
    Nov 20 at 4:14











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%2f53385134%2fregex-for-value-inside-mustache-braces-while-ignoring-erroneous-whitespace%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









2














One option is to scan your input string into an array matches, using the following pattern:



{+s*([^{}]+)s*}+


This would capture all content appearing in between (presumably) nested sets of curly braces. Then, we can flatten that array into a single level array of string matches, from the single capture group, and collect, removing leading and trailing whitespace.



str = "Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun  fact    }}"
arr = str.scan(/{+s*([^{}]+)s*}+/)
print arr.flatten.collect{|x| x.strip || x }

["name", "greeting", "code question website!!! ?? ?", "random fun fact"]





share|improve this answer





















  • That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
    – Genhain
    Nov 20 at 3:38






  • 2




    Well that isn't what your question says, and you should not change your question after others have already answered.
    – Tim Biegeleisen
    Nov 20 at 3:42






  • 2




    @Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
    – anothermh
    Nov 20 at 3:57










  • @TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
    – Genhain
    Nov 20 at 4:10










  • Ask a new question and reference this one, and maybe use my pattern if that would help at all.
    – Tim Biegeleisen
    Nov 20 at 4:14
















2














One option is to scan your input string into an array matches, using the following pattern:



{+s*([^{}]+)s*}+


This would capture all content appearing in between (presumably) nested sets of curly braces. Then, we can flatten that array into a single level array of string matches, from the single capture group, and collect, removing leading and trailing whitespace.



str = "Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun  fact    }}"
arr = str.scan(/{+s*([^{}]+)s*}+/)
print arr.flatten.collect{|x| x.strip || x }

["name", "greeting", "code question website!!! ?? ?", "random fun fact"]





share|improve this answer





















  • That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
    – Genhain
    Nov 20 at 3:38






  • 2




    Well that isn't what your question says, and you should not change your question after others have already answered.
    – Tim Biegeleisen
    Nov 20 at 3:42






  • 2




    @Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
    – anothermh
    Nov 20 at 3:57










  • @TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
    – Genhain
    Nov 20 at 4:10










  • Ask a new question and reference this one, and maybe use my pattern if that would help at all.
    – Tim Biegeleisen
    Nov 20 at 4:14














2












2








2






One option is to scan your input string into an array matches, using the following pattern:



{+s*([^{}]+)s*}+


This would capture all content appearing in between (presumably) nested sets of curly braces. Then, we can flatten that array into a single level array of string matches, from the single capture group, and collect, removing leading and trailing whitespace.



str = "Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun  fact    }}"
arr = str.scan(/{+s*([^{}]+)s*}+/)
print arr.flatten.collect{|x| x.strip || x }

["name", "greeting", "code question website!!! ?? ?", "random fun fact"]





share|improve this answer












One option is to scan your input string into an array matches, using the following pattern:



{+s*([^{}]+)s*}+


This would capture all content appearing in between (presumably) nested sets of curly braces. Then, we can flatten that array into a single level array of string matches, from the single capture group, and collect, removing leading and trailing whitespace.



str = "Hello {{{ name }}}, {{{greeting}}}. what is your favourite code related website {{ code question website!!! ?? ? }}? here is some random fact {{ random fun  fact    }}"
arr = str.scan(/{+s*([^{}]+)s*}+/)
print arr.flatten.collect{|x| x.strip || x }

["name", "greeting", "code question website!!! ?? ?", "random fun fact"]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 at 2:09









Tim Biegeleisen

216k1386138




216k1386138












  • That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
    – Genhain
    Nov 20 at 3:38






  • 2




    Well that isn't what your question says, and you should not change your question after others have already answered.
    – Tim Biegeleisen
    Nov 20 at 3:42






  • 2




    @Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
    – anothermh
    Nov 20 at 3:57










  • @TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
    – Genhain
    Nov 20 at 4:10










  • Ask a new question and reference this one, and maybe use my pattern if that would help at all.
    – Tim Biegeleisen
    Nov 20 at 4:14


















  • That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
    – Genhain
    Nov 20 at 3:38






  • 2




    Well that isn't what your question says, and you should not change your question after others have already answered.
    – Tim Biegeleisen
    Nov 20 at 3:42






  • 2




    @Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
    – anothermh
    Nov 20 at 3:57










  • @TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
    – Genhain
    Nov 20 at 4:10










  • Ask a new question and reference this one, and maybe use my pattern if that would help at all.
    – Tim Biegeleisen
    Nov 20 at 4:14
















That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
– Genhain
Nov 20 at 3:38




That is useful, but the end result I'm looking for is to also have the original string while matching the aforementioned characters and modifying/replacing them with base64 encoded versions of the tag. i am currently using gsub gsub(/({{2,3}s*)([^{}]*)(s*}{2,3})/) { |_| $~[1]+Base64.urlsafe_encode64($~[2].strip, padding: false)+$~[3] }
– Genhain
Nov 20 at 3:38




2




2




Well that isn't what your question says, and you should not change your question after others have already answered.
– Tim Biegeleisen
Nov 20 at 3:42




Well that isn't what your question says, and you should not change your question after others have already answered.
– Tim Biegeleisen
Nov 20 at 3:42




2




2




@Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
– anothermh
Nov 20 at 3:57




@Genhain Why would you post a question that isn't your actual question? I recommend you read How do I ask a good question? and remember that you're asking volunteers to help you.
– anothermh
Nov 20 at 3:57












@TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
– Genhain
Nov 20 at 4:10




@TimBiegeleisen and anothermh. Thank you for your time, sorry i was not succinct enough.
– Genhain
Nov 20 at 4:10












Ask a new question and reference this one, and maybe use my pattern if that would help at all.
– Tim Biegeleisen
Nov 20 at 4:14




Ask a new question and reference this one, and maybe use my pattern if that would help at all.
– Tim Biegeleisen
Nov 20 at 4:14


















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.





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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385134%2fregex-for-value-inside-mustache-braces-while-ignoring-erroneous-whitespace%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?