1

Give some solution to this following example,

Scenario-1:

My String : Password={my_pswd}}123}

I want to select the value enclosed within the {} brackets(Example: I want to select the complete password key value {my_pswd}123} not {my_pswd})

If I'm using this regex \{(.*?)\} , this will select {my_pswd} not {my_pswd}}123}. So how to get complete word even if the word has } in between? Give me some suggestions by using regex or any other way.

Scenario-2: I am using this regex ^\{|\}$ . If my string have both { bracket and } bracket like this {{my_password}} then only it want to select first and last bracket. If my string like this {{my_password, it don't want to select that starting bracket. Its like AND condition in Regex. I referred many posts they did with look up but I can't get clear idea. Give me some suggestion.

Thanks.

4
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jul 1, 2018 at 2:16
  • See my answer, there is a demo, everything works as expected. If you test the patterns anywhere at online testers, use ^\{([\s\S]*)\}$ or /^\{(.*)\}$/gs Commented Jul 1, 2018 at 5:19
  • You are using the wrong option at regex101.com, why at all are you using it? See regex101.com/r/qCwcfi/2 with the JS option selected, it shows the regex works well for that right flavor. And see ideone.com/5wgIzu, it proves the regex is working with std::regex. Commented Jul 1, 2018 at 6:12
  • @WiktorStribiżew, Thanks for your clear explanation. Now I got it and solved. Commented Jul 2, 2018 at 7:59

1 Answer 1

1

It seems that the {...} substrings you want to match must be followed with ; or end of string. This will not work for cases when a } inside the values can also be followed with ;.

You may solve the first issue by adding a (?![^;]) lookaround:

\{(.*?)\}(?![^;]) 

See the regex demo.

Details

  • \{ - a { char
  • (.*?) - Group 1: any 0+ chars as few as possible
  • \} - a } char
  • (?![^;]) - no char other than ; is allowed right after the current position

See the C++ demo:

#include <iostream> #include <vector> #include <regex> int main() { const std::regex reg("\\{(.*?)\\}(?![^;])"); std::smatch match; std::string s = "Username={My_{}user};Password={my_pswd}}123}}}kk};Password={my_pswd}}123}"; std::vector<std::string> results( std::sregex_token_iterator(s.begin(), s.end(), reg, 1), // See 1, it extracts Group 1 value std::sregex_token_iterator()); for (auto result : results) { std::cout << result << std::endl; } return 0; } 

Output:

My_{}user my_pswd}}123}}}kk my_pswd}}123 

As for the second scenario, you may use

std::regex reg("^\\{([^]*)\\}$"); std::string s = "{My_{}user}"; std::cout << regex_replace(s, reg, "$1") << std::endl; // => My_{}user 

See another C++ demo.

The \{([^]*)\}$ pattern matches the { at the start (^) of the string, then matches and captures into Group 1 (later referenced with the help of $1 in the replacement pattern) any 0+ chars, as many as possible, and then matches a } at the end of the string ($).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.