This is one of those issues that could be handled in myriad ways, so I’m going to stick with bbedit but otherwise just focus on your specific example. Hopefully this will give you some ideas as to how to proceed.
First, bbedit is extra nice because unlike pretty much any other app, it is recordable. This means that you can open a document in bbedit, create a new script in script editor, click record, and then switch back to bbedit and most of what you do will generate code in the new script. This is especially useful when it comes to learning how to script searches.
Try this: Make a bbedit document that has your provided text as its entire content and save it as ‘37.html’, and then begin recording a script as outlined above. Then switch back to bbedit and select the text of the first row (but not the line ending) and then type command-e, which will make the selection into the Find string. Then type command-f to open the find dialog and you should see some variation on your selection in the find box. Check the ‘grep’ box and hit return (or click Next) — this will ‘find’ the selected text. Then switch back to the script and stop the recording. Your script should look something like this:
tell application "BBEdit"
find "<page page-num=\"37\\.1\">" searching in text 1 of text document "37.html" options {search mode:grep, wrap around:true} with selecting match
end tell
The options can be looked up in the bbedit dictionary but generally they correspond to the options in the find dialogue. Make sure that your script line includes ‘with selecting match’. Then, change each of the three digits in the script to ‘\d’. The find string in the script should then look like ‘<page page-num="(\d\d\.\d)">’. NB in a grep search, at least in bbedit, ‘\d’ represents a single digit. Applescript requires an extra ''. So basically, it will search for your ‘XX.X’ pattern.
If you run the script at this point, you will find that the text of the first line will be selected. You then want to run another search but only within the selected text. This time, set the search string to match just the XX.X pattern. The script line should look like this:
find "\\d\\d.\\d" searching in selection options {search mode:grep} with selecting match
If you run your script now, you should end up with just the ‘37.1’ selected. So let’s assign this to a variable:
set ts to the selection
Now that we have the text to insert, we can set up the replacement, which might look something like this:
set repStr to "<div class=\"imageGalleryPage\" "
find repStr searching in text 1 of text document 1 options {search mode:grep} with selecting match
replace repStr searching in selection options {search mode:grep} using repStr & "id=\"" & ts & "\" "
Note that I’ve replaced the document name with its index. Before you run the script, in bbedit move the cursor to the very beginning of the document. What this does is look for the beginning of the div tag and find and select the first attribute. It then replaces all that with itself and the id attribute.
Now, there are various issues like, what happens to the second XX.X? If you have multiple text blocks like the one provided, and the cursor is set to the beginning, then each time you run the script, it should insert the id attribute for one instance. You’ll have to figure out the rest, or at least provide more information. Hope this at least gets you going.