By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Alex,
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\"");


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
Jul 19 '05 #2

P: n/a
Alex wrote:
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 \" ....
How do you identify a "substring"? You need to be more specific.
Could someone point me in the right direction..


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

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.