Hi all, I'm a new Perl user doing some on-the-job-training. I'm writing a script to edit a text file. I'm using a series of substitutions in my script to modify the data in an array variable and I've run into a road block. I've done the following that works.

Text example: <pick;1-1;0;0>

Intention: Delete any <pick> code, match "<pick" any number of any character up to and including ">"

Code

s/<pick(.*)>//g for @data01;

So I tried something similar to match "<DOC>" and any character not a "{", up to and including next "{" and replace it with "{".

Text example: <DOC>50FWRMMED84526R CD-5112-88<pa>{I}

Between the "<DOC>" and the "{" could be anything. I'm trying to match the "{" to give it an ending "anchor". The "{" marks the beginning of the next piece of data. I've tried more combinations than I can remember but I think this was my initial trial.

Code

s/<DOC>(.*[^\{])\{/\{/g for @data01;

Can some one help me? What am I doing wrong? How would I write a match that would: Match "<DOC>" and any character not a "{", up to and including the first occurrence of "{" and replace it with "{".

Sccond thought: I thought trying to match any character not a "{" and then including a "{" might be throwing it off so I tried the following with no success:

Most of the time, you should avoid the ".*" combination, which is dangerous, as it often matches more than what you want. You should either have a non-greedy (or frugal) quantifier ".*?" or use a negative character class instead of the ".".

I'm using O'Reilly's "Programming Perl". According to the book the "Dirty Dozen" that require escaping are:

\ | ( ) [ { ^ $ * + ? .

You are right! When my sample code "worked" (Did you run it as posted?), I did not bother to check the documentation. My "{" should be escaped. Note that Laurent got away with the same mistake in his final example.

Please post a complete example (similar to mine). Show us the exact output that you expect. All of the responses so far do solve the problem as we understand it. Good Luck, Bill

FishMonger, your method worked. To be safe I escaped the "{". I also tried adding a non greedy addition but it didn't seem necessary. The whole thing was to find a <DOC> and delete everything between that and the first occurrence of a "{". That code also worked for the <DOC> that came at the end of the string.

Many sources I have viewed/read they state how in Perl, there is more than one way to do it. That can get confusing for us beginners.