Reputation: 567
Said a input string
std::string input_string = ";;abc,123;,,;456,def;789,ghi,135,jkl";
I want to get only "number,string" pair within semicolon closure from input string. In this case, "456,def" is the only match.
Here is my code
std::regex regex("(\\d+),([^,;]+)(?:;|$)");
for (std::sregex_iterator it(input_string.begin(), input_string.end(), regex), itEnd; it != itEnd; ++it) {
std::cout << it->str(1) << ", " << it->str(2) << std::endl;
}
But this code also get "135,jkl" as result
Please help me how to modify the regex string?
Upvotes: 2
Views: 135
Reputation: 11940
So add a non-capturing group for the leading semicolon:
std::regex regex("(?:^|;)(\\d+),([^,;]+)(?=;|$)");
Or, since you're iterating through them anyway, do it in AWK way!
// iterate through ;-separated fields
for(std::sregex_iterator it(
input_string.begin(), input_string.end(), std::regex("[^;]+"))
, itEnd
; it != itEnd
; ++it)
{
if(std::regex_match(it->str(0), std::regex("\\d+,[^,]*"))) {
std::cout << it->str(0) << std::endl;
}
}
Upvotes: 1
Reputation: 9619
Seems like you are complicating things!
The following simple regex works just fine:
;(\d+,[a-zA-Z]+);
All I am looking for is more than one digit, followed by a comma, followed by more than one alphabet; which is enclosed within semi-colon on either side. Make sure the global
flag is turned on to capture all such cases.
P.S.: I assumed that the string part will contain only English alphabets (a to z, case insensitive).
Upvotes: 0