CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2005
    Posts
    281

    Boost Replace Function

    Hey everyone, I'm messing around with boost (learning about their lexical parser anything Boost can do with strings), and I can use the replace_all function, but is there a function to replace only the current instance of the word that the line is reading?

    For example, when I use the following code:

    Code:
        while ( !Input.eof ( ) )
        {
            getline ( Input, String );
            boost::replace_all ( String, "test2", ToReplaceStringWith );
            outf << String << endl
        }
    of course it replaces every instance of "test2" in the text file. But I actually want to replace "test2" only line by line, rather than all at once. That way, lets say I can change "test2" to "test22" in the first instance, and the next time I run into it I can change "test2" to "aweometest" or something of the sort.

    I looked a little into regex_replace, but would that do what I want? How would I replace the replace_all function with regex_replace if it will do what I want?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Boost Replace Function

    Quote Originally Posted by Rehorav View Post
    But I actually want to replace "test2" only line by line, rather than all at once.
    That's exactly what your code does.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2005
    Posts
    281

    Re: Boost Replace Function

    How would I make it stop after the first occurrence though?

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Boost Replace Function

    Quote Originally Posted by Rehorav View Post
    How would I make it stop after the first occurrence though?
    boost::replace_first
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured