I have a selection of text that looks like the following. I need to do a rudimentary edit on it, but can't fathom the regex that I need. Maybe it's just been a long day, and I'm not seeing what I need.
Sample data:
START ITEM = 1235
BEGIN
WORD
RATE = 98
MORE WORDS
CODE = XX
STUFF
END
BEGIN
TEXT
MORE WORDS
RATE = 57
ADDITIONAL TEXT
CODE = YY
OTHER THINGS
END
STOP
START ITEM = 9983
BEGIN
WORD
RATE = 01
MORE WORDS
CODE = AA
STUFF
END
BEGIN
TEXT
MORE WORDS
RATE = 99
ADDITIONAL TEXT
CODE = XX
OTHER THINGS
END
STOP
I'm given a CODE
and an ITEM
number, and need to edit the rate in the appropriate BEGIN
/END
section. Fortunately, the sections are well-defined with STOP
/START
BEGIN
/END
(they're keywords, and aren't anywhere else).
My toolbox for this is Perl regular expressions.*
The first solution I tried didn't work (values hard-coded):
$tx =~ s/(START \s ITEM \s = \s 9983.*?
BEGIN
.*?
RATE \s = \s )\d+
(.*? # Goes too far
CODE \s = \s XX)
/$1$newRate$2
/sx;
Because the indicated code winds up matching too much, finding the correct code farther down but always editing the first entry.
Suggestions?
*
The actual code relies on the regex being added onto a stack of regexes (sort of a post-processing filter) that are each applied in turn to the text to do edits. Heck, I could do a full-on parser if I had the text. But I was hoping not to have to break that code open and stick with the API I've got.