Please help, I'm a newb at perl and I can't figure this out.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    Please help, I'm a newb at perl and I can't figure this out.

    Basically I want to parse some C++ source code and replace some
    constants.

    I've mangaed to read in the file and replace some of the things that
    need replacing. but I'm having a hard time matching a particular
    pattern. For example:

    cout << "This is a \"String\"" << "With some text";
    printf("This is another possible \"string\"") ;

    needs to become:

    cout << L"This is a \"String\"" << L"With some text";
    wprintf(L"This is another possible \"string\"") ;

    So I need a pattern that will match only the first " of every
    substring but not match the final " or \" ....

    Could someone point me in the right direction.. thanks for the help..

    Alex
  • Berk Birand

    #2
    Re: Please help, I'm a newb at perl and I can't figure this out.

    Hi Alex,
    [color=blue]
    > Basically I want to parse some C++ source code and replace some
    > constants.
    >
    > I've mangaed to read in the file and replace some of the things that
    > need replacing. but I'm having a hard time matching a particular
    > pattern. For example:
    >
    > cout << "This is a \"String\"" << "With some text";
    > printf("This is another possible \"string\"") ;
    >
    > needs to become:
    >
    > cout << L"This is a \"String\"" << L"With some text";
    > wprintf(L"This is another possible \"string\"") ;[/color]

    If you translate word by word what you said, the RE would be something
    like
    s/\b"/L\1/

    (the w before printf is a mistake I suppose, for you'll need another rule
    for that)
    This would get the first two, but not the third one, since there
    isn't a space between the parenthesis and the quotes. So you either put
    one, or some other helpful friend would devise a more clever solution.
    I couldn't quite understand what you meant by 'the first " of every
    substring'.

    Also, if these are the only changes you wish to make, then using sed
    instead of perl would be a more appropriate solution. Every tool has its
    pros and cons. It is true it is perfectly possible to use Perl. Yet it
    would be needless to add the shabang, the code for reading, the code
    for replacing, and finally for writing the file. Instead you can just use
    one elementary command with sed and you are done.
    The two reasons for using Perl would be for learning purposes, or if the
    program does something else involving calculations or iterations.


    HTH
    BB

    Comment

    • Gunnar Hjalmarsson

      #3
      Re: Please help, I'm a newb at perl and I can't figure this out.

      Alex wrote:[color=blue]
      > For example:
      >
      > cout << "This is a \"String\"" << "With some text";
      > printf("This is another possible \"string\"") ;
      >
      > needs to become:
      >
      > cout << L"This is a \"String\"" << L"With some text";
      > wprintf(L"This is another possible \"string\"") ;
      >
      > So I need a pattern that will match only the first " of every
      > substring but not match the final " or \" ....[/color]

      How do you identify a "substring" ? You need to be more specific.
      [color=blue]
      > Could someone point me in the right direction..[/color]

      These might help you get started:

      s/(<<\s*)(")/$1L$2/g;
      s/(printf\s*\(\s* )(")/w$1L$2/g;

      --
      Gunnar Hjalmarsson
      Email: http://www.gunnar.cc/cgi-bin/contact.pl

      Comment

      Working...