string parsing

i was learning string parsing using c++.
i came across a problem where there are many delimiters and specific string needs to be selected from the given successive line

input was like this ans we need to filter computer name , date and connection status from it .


[0BBB] ComputerName:Acer UserID:A90 Laua Lon Station 9 LanId: | (12/21 12:30:49) | Client disconnected.
[0BBB] ComputerName:Samsung UserID:A01 Laua Long Station 9 LanId: | (12/21 12:30:54) | Client connected.



and many more strings

please can anyone help in how to separate computer name, date and connection status from these successive strings.
Last edited on
if you have that whole thing as one string, you can use repeated calls to find() to locate the keywords and then pull out what is between keywords or spaces or whatever the back side delimiter is using a substring.

String parsing and/or separation is usually done in (1) split function, or (2) regular expression.

> split (boost library)
<https://www.boost.org/doc/libs/1_57_0/doc/html/string_algo/usage.html>

probably, delimiter (eCompress argument) is set correctly, you get what you want.

> regex (C++11, it is standard library)

Regular expression is a little confusing, but after you are familiar with it, you can handle more situation than the above.
Last edited on
The information, as given in your post, is not clearly delimited.
I would think that the individual fields would be TAB-delimited, but without that I cannot make any assumption.

You can still get the named elements easily enough.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <ciso646>
#include <iostream>
#include <sstream>
#include <string>

std::string trim( const std::string& s )
{
  auto b = s.find_first_not_of( " \f\n\r\t\v" );
  auto e = s.find_last_not_of ( " \f\n\r\t\v" );
  if ((b == s.npos) or (b > e)) return "";
  return s.substr( b, e - b + 1 );
}

std::string substr( const std::string& s, std::string::size_type begin, std::string::size_type end )
{
  return s.substr( begin, (end == s.npos) ? s.npos : (end - begin) );
}

int main()
{
  std::string strings[] =
  {
    "[0BBB] ComputerName:Acer UserID:A90 Laua Lon Station 9 LanId: | (12/21 12:30:49) | Client disconnected.",
    "[0BBB] ComputerName:Samsung UserID:A01 Laua Long Station 9 LanId: | (12/21 12:30:54) | Client connected."
  };

  for (auto s : strings)
  {
    auto cn_idx     = s.find( "ComputerName:" );
    auto uid_idx    = s.find( "UserID:" );
    auto lid_idx    = s.find( "LanId:" );
    auto time_idx   = s.find( '|' );

    std::cout << "\n";
    if (cn_idx  != s.npos) std::cout << "Computer Name = \"" << trim( substr( s, cn_idx  + 13, uid_idx  ) ) << "\"\n";
    if (uid_idx != s.npos) std::cout << "User ID       = \"" << trim( substr( s, uid_idx +  7, lid_idx  ) ) << "\"\n";
    if (lid_idx != s.npos) std::cout << "LAN ID        = \"" << trim( substr( s, lid_idx +  6, time_idx ) ) << "\"\n";
  }
  std::cout << "\n";
}

Enjoy!
@Duthomas
thank you for your help , those stl functions were very helpful.
Topic archived. No new replies allowed.