Thread: Parsing Text

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    15

    Parsing Text

    Okay, I just want to be able to easily parse a string, but I have -no- idea how... I know someone else will though!

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    write a simple finite state machine

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    15
    -beats his head on his keyboard-
    Huh?

  4. #4
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    a vague question deserves a vague answer

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use stringstream or find() and substr().

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Use a regular expression.

    rofl rocky

  7. #7
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    Quote Originally Posted by Daved
    Use stringstream or find() and substr().
    sounds slow, a FSM would be much faster, do things in one pass is always better than multiple find calls

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> a FSM would be much faster, do things in one pass is always better than multiple find calls

    I disagree. It depends on the situation, and as you noted this situation is quite vague. There's no reason to spend time or effort coding an FSM when a simple solution will work just as well. Premature optimization and all that.

  9. #9
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    fsms are simple to write, one for loop, check a few states. usually when someone says they want to "parse text", they have something like "field1;field2;field3;field4;...;field20;". In this case it would be simple to write an fsm.

  10. #10
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    If the input is like rockytriton describes, you could just use a stringstream object to parse the text in one pass.

    Code:
    #include <iostream>  
    #include <string>    
    #include <sstream>   
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      string text = "field1;field2;field3;field4;field20;" ;
      
      stringstream ss(text);
      
      while( ss )
      {
        string field ; 
        getline( ss, field, ';' );
        cout << field << "\n" ;
      }
      
      cin.get();  
      return 0 ;
    }
    Output:

    Code:
    field1
    field2
    field3
    field4
    field20

  11. #11
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    there you go, even better

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> In this case it would be simple to write an fsm.

    Maybe I'm not understanding what you mean. Could you convert Dante Shamest's program to use a finite state machine?

    >> even better

    That uses a stringstream, so I don't understand your problem with my original suggestion.

  13. #13
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    ok, that was a bad example, now that I started writing an fsm for it, I relaized it doesn't need more than 1 state. it would make more sense for something that would parse data out of html tags, that's the last thing I did that I used an fsm to implement. In this case, it's still pretty simple and would be much faster than running through multiple finds for '<' and ">' or '/>'.

    >> That uses a stringstream, so I don't understand your problem with my original suggestion.

    no problem really, I didn't know about the changing the delimiter feature of getline(), it was really the find() and substr() part, it just makes me think of looping through with multiple finds.

  14. #14
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    it would make more sense for something that would parse data out of html tags, that's the last thing I did that I used an fsm to implement.
    Ah yes. If the input was HTML, a FSM would work very well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Help parsing text file
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2008, 10:21 AM
  3. Problem parsing comments and such in text file
    By zaxxon in forum C Programming
    Replies: 3
    Last Post: 08-09-2004, 12:14 AM
  4. Text file parsing
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 01:17 AM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM