Regex Help

Hello

I am working on a problem that involves using Boost Regex

I have a string 66{Test1}{Class}.zpl

I need to use Boost Regex to :
1) Get the length of the string (in may vary)
2) return substrings
a) Beginning substring - 66
b) End Substring - .zpl
3) The values inside the braces can vary, they are not literals, I need to return the contents of the variables
4) Finally i need to create a new string with the new values
Example -> Beginning Substring + Value of Test1 + Value of Class + End Substring

I understand this can be accomplish via built in methods.

Thanks

So you definitely want to use boost::regex and not std::regex ?

I need to return the contents of the variables

What exactly does that mean?
Presumably when you create the string you will fill it with the "contents of the variables".
Or are you saying that the variable names themselves are in the string between the braces?
Hi tpb

The input string is hardcoded and during runtime the values that are in the {} curly braces will vary.
Hardcoded values (In Ini file) - 66{Test1}{Class}.zpl
RunTime value example - 66{Test1}{Math123}.zpl
I need to get the values during runtime.

I hope that helps ..

Thanks
I still don't understand how a hardcoded string can very.
You'll have to show some code and example input/output.
Is the problem with Boost.Regex or regular expressions in general?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <boost/regex.hpp>

#include <iostream>
#include <string>
#include <iterator>

int main()
{
  std::string s = "66{Test1}{Math123}.zpl";
  // Note: a raw string literal is used to avoid having to escape values more than once
  boost::regex re{R"(^([^{]*)\{([^}]*)\}\{([^}]*)\}(.*)$)"};

  boost::smatch base_match;

  if (! boost::regex_match(s, base_match, re))
    return 1;
    
  for (std::size_t i = 1; i < base_match.size(); ++i) {
    boost::ssub_match sub_match = base_match[i];
    std::string piece = sub_match.str();
    std::cout << "  submatch " << i << ": " << piece << '\n';
  }   
}


Live demo:
https://wandbox.org/permlink/lc1Mwg4zfNsTXvsh

See:
https://en.cppreference.com/w/cpp/regex/regex_match
Last edited on
Hi

mbozzi That is exaclty what I needed thank you for your help.

tbp the hardcoded string lives on an Ini file, the string values within the braces are changed in a application that recieves information to populate the contents within the braces.
Also tbp thank you for your help also..



Topic archived. No new replies allowed.