I agree that the RE is ugly -- for example [\d\D] could just be expressed as . but the *? is a lazy quantifier, so won't match everything and then backtrack.
(To the poster -- I highly recommend picking up the book "Mastering Regular Expressions" if you're going to use regular expressions on a regular basis.)
update: To the poster, a couple questions. Do you get any output from this or does it just die? (You're printing to STDOUT, not your OUTFILE anyway, so if you get no output at all, then this is dying before it ever hits a print statement.) How big is stranger.xap in bytes, not lines? Have you run this code using the perl debugger and seen where/how it blows up?
Looking at your code, it looks like you're pulling all of your XAPFILE into $file. Is there a reason you need to do that versus going line by line and saving into a new output file rather than overwriting your old one? Your regular expressions are ending with newlines anyway. (Data spans multiple lines and is semicolon terminated? Is that why you're using [\d\D]*?; in your RE or is your priority just a short number?)
It would definitely help to post a few examples of data from stranger.xap and temp.txt and explain what you're trying to do to make sense of your code and help you out.
update 2:Looking closer at your RE's, I think they aren't doing what you think. Look at the two RE's -- just the matching part -- next to each other. (I've replaced the "\ " with "\s" for clarity here:)
(Sound\s*\{\s*Name\s=\s$Sound;\s*Priority\s=\s)([\d\D]*?);
(Sound\s*\{(\s*)Name\s=\s$Sound;\s*$)
These differ in a couple ways. Obviously, the first is looking for the case where a "Priority" exists, but the second also is capturing any potential spaces at a certain point with (\s*). Why? Also, the second one terminates at an end of line (since you use the "/m" modifier and have a "$"), but the first terminates just at the semicolon. Again, why? Do these lines wind up differing by more than just whether or not a Priority exists?
Then look at your replacement parts, again next to each other.
$1$Pri;
$1$2Priority\ =\ $Pri;\n
First of all, those "\ " in the second line will show up literally in the replacement -- not what you want. You're also only terminating with a "\n" on the second one -- that doesn't seem right, either. And the "$1$2" on the second line is only necessary because you're capturing those spaces. Overall, making some assumptions about what you're trying to do, it seems like these two could be combined pretty easily as:
s/(Sound\s*\{\s*Name\s=\s$Sound).*?;/$1; Priority = $Pri;/m
I still think that probably won't do what you want, as I don't understand how your data spans lines (or not) or really what your data format is, but I think the RE above is a cleaner version of what your existing RE's are trying to do.
-xdg
Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.
|