470,648 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

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

Similar topics

reply views Thread by trncyng | last post: by
2 posts views Thread by David K. Worman | last post: by
7 posts views Thread by Sam Lowry | last post: by
4 posts views Thread by javascript_noob12 | last post: by
24 posts views Thread by Apotheosis | last post: by
23 posts views Thread by Tiny Tim | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.