a simple regexp match using stl algorithms

P

persres

Hi,
I have a string. I want to look for the substring that matches
regexp (*).

That is :
string str("abc + (inside brackets)dfsd";

I want to get the substring ---- (inside brackets).

Is it possible using string member functions and algorithms?
How about the same functionality for any vector.

I believe we could use boost.regex. But I am just learning stl algos
thoroughly. I am wondering if there is any nice way using some calls
such as substring, find ,search, adjacent etc.
Thanks
 
Ö

Öö Tiib

Hi,
  I have a string. I want to look for the substring that matches
regexp (*).

That is :
string str("abc + (inside brackets)dfsd";

Is this line meant as example string or code that constructs example
string? If code then it misses one closing bracket; if example string
then your problem description lacks explanation what to do when
parentheses mismatch (or when there are multiple substrings in
brackets or when such substrings are nested).
I want to get the substring ----  (inside brackets).

Is it possible using string member functions and algorithms?

Yes. Boost.Regex is better suited but you can definitely do it
without. Try and if you meet any difficulties then post your problem.
How about the same functionality for any vector.

Certainly possible, but 'any vector' likely contains anything. So you
need to define what is considered a 'left bracket' and what is
considered a 'right bracket' among specific vector's elements. Most
regex libraries deal with character arrays or strings however. You
better first try with string (or wstring), then with vector<char> or
vector said:
I believe we could use boost.regex. But I am just learning stl algos
thoroughly. I am wondering if there is any nice way using some calls
such as substring, find ,search, adjacent etc.

Sure. Try it out. There are lot of nice ways. Only that first you need
to decide what to do with mismatch, multiple and nested brackets.
 
H

Howard Hinnant

Hi,
  I have a string. I want to look for the substring that matches
regexp (*).

That is :
string str("abc + (inside brackets)dfsd";

I want to get the substring ----  (inside brackets).

Is it possible using string member functions and algorithms?
How about the same functionality for any vector.

I believe we could use boost.regex. But I am just learning stl algos
thoroughly. I am wondering if there is any nice way using some calls
such as substring, find ,search, adjacent etc.
Thanks

There is nothing in C++03 to help you do this. Boost::regex is a good
library to look to for this functionality. For the next C++ standard
(C++0x), what is now boost::regex has been mostly standardized and is
now std::regex. Here is an example C++0x program:


#include <iostream>
#include <regex>
#include <string>

int
main()
{
try
{
std::string str("abc + (inside brackets)dfsd");
std::smatch m;
std::regex_search(str, m, std::regex("\\([^\\)]*\\)"));
if (m[0].matched)
{
std::cout << "Found " << m.str(0) << '\n';
}
else
{
std::cout << "Not found\n";
}
}
catch (std::exception const& e)
{
std::cout << e.what() << '\n';
}
}

Found (inside brackets)

-Howard
 
G

gwowen

Hi,
  I have a string. I want to look for the substring that matches
regexp (*).

So "()", "(()", "((()" and so on? Or do you mean (.*)?
If so, what would you expect to be extracted from "This string (has
(mismatched parentheses)"
What about "This string (has (matched parentheses))"?
What about "(setq foo\n(cdr bar))"?
 
P

persres

So "()", "(()", "((()" and so on?  Or do you mean (.*)?
If so, what would you expect to be extracted from "This string (has
(mismatched parentheses)"
What about "This string (has (matched parentheses))"?
What about "(setq foo\n(cdr bar))"?

Sorry there are no mismatched brackets. It doesnt have to brackets.
Just looking for a sequence like - a,...,b.
So, looks like I need boost. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top