regex_match issue

I never used regex in cpp and I'm having issue with the following sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	// Get line length until ';' character with regex
	std::wstring line = L"string 1 ; string 2";
	unsigned linelength = 0;

	if (!line.starts_with(L";"))
	{
		std::wregex reg(L";");
		std::wsmatch match;

		if (std::regex_match(line, match, reg))
		{
			const std::ptrdiff_t len = match.position();
			linelength = len > linelength ? len : linelength;
		}

		std::cout << linelength;
	}
}


I know I could use
std::size_t pos = line.find(L';');

But this is just an example of larger portion, I need it with regex.

Basically I need line length excluding anything beyond and including ";"

**EDIT:**

I figured out that I need to use std::regex_search instead.

What an unexpected behavior...
Last edited on
Registered users can post here. Sign in or register to post.