ZoOl007
ZoOl007

Reputation: 417

match strings between outer single quotes

I have the following data:

compose '`' 'A' to '\C0'
compose '`' 'a' to '\E0'
compose '\'' 'A' to '\C1'
compose '\'' 'a' to '\E1'
compose '^' 'A' to '\C2'
compose '^' 'a' to '\E2'

All the quotes are single quotes

I have this regex

\'(\\.|[^\'])*\'

It matches what I want in the full matches but it includes the outer single quotes.

This string compose '\'' 'A' to '\C1' gives me '\'' and 'A' and '\C1' but I need \' and A and \C1

I could get away with removing the first and last single quote from the string but I'd like to do it with regex.

How can I obtain the result I want please?

As for the regex engine, it's for use in a qt5 core application, so c++

Upvotes: 0

Views: 105

Answers (3)

Alan Birtles
Alan Birtles

Reputation: 36469

Your regex needs a slight modification, capturing a group multiple times doesn't really work. What you really want is a group containing zero or more copies of your \\.|[^\'] expresssion. You can do this with a non capturing group which is written by adding ?: inside the opening parenthesis of the group. The full regex is then:

\'((?:\\.|[^\'])*)\'

You can try it out on regex101.

Upvotes: 1

Timo
Timo

Reputation: 9835

Your regex is not optimal. I don't know what exactly you are allowed to match, but from the data that you have given us this regex will do the trick: \s\'(\S+?\'?)\'.

std::regex reg(R"(\s\'(\S+?\'?)\')");
std::string input = R"(
compose '`' 'A' to '\C0'
compose '`' 'a' to '\E0'
compose '\'' 'A' to '\C1'
compose '\'' 'a' to '\E1'
compose '^' 'A' to '\C2'
compose '^' 'a' to '\E2')";

auto begin = std::sregex_iterator(input.begin(), input.end(), reg);
auto end = std::sregex_iterator();
for (auto it = begin; it != end; ++it)
    std::cout << (*it)[1].str() << '\n';

Here is a full example.

Upvotes: 1

Yksisarvinen
Yksisarvinen

Reputation: 22334

You already have a group in your regex - you can read it.

If you have std::smatch called results (or whatever you use as std::match_results), then results[1] will give you the first (and only) group - provided that string was actually matched, otherwise it's UB.

std::regex r {"\'(\\.|[^\'])*\'"};
std::string input = "compose '`' 'A' to '\\C0'";
std::smatch results;
if(std::regex_search(input, results, r)) {
    std::cout << results[0] << std::endl  //full match
              << results[1] << std::endl; //first group
}

Upvotes: 0

Related Questions