472,801 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,801 software developers and data experts.

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 1685
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: trncyng | last post by:
Is there a detailed description of how to use Informix from within CPython in Unix? I could not figure out how to use informixdb, dbi.py or mxodbc. Up to this point, Python has been a breeze to...
2
by: David K. Worman | last post by:
So this is probably a simple thing I'm missing, but then I just started working with perl tonight - and of course I bought the Camel book, and conveniently left it at work on my desk where it does...
3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
7
by: Sam Lowry | last post by:
Greetings. I am trying to do something which should elementary for Perl, but I have only been able to find bits and pieces on it. When I put the bits together they do not work. Maybe I am going...
4
by: javascript_noob12 | last post by:
Hi. I am trying to create a CGI script that will use Perl to read a text file to get a list of values, then turn that list into a menu on a website. The menu will be a form with a JavaScript...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
23
by: Tiny Tim | last post by:
I am new C program student. I wonder can we write a program in C that just trigger its function to do a certain action ? In this case, I have a program but it is not working. What I want is for...
5
matrekz42
by: matrekz42 | last post by:
Hello there, I currently have a form that has a button to run multiple queries and I want to add a bar to show the user the progress. I tried using the Microsoft Progress Bar Control 6.0, but I'm...
0
numberwhun
by: numberwhun | last post by:
Please remember the following when posting a question in the Perl Forum. Doing so will get you better assistance. Provide Sample Code - You will find that this is asked for more often than not....
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.