-1

Here is what ive been working with sofar...

This is what im using in the find section - ([img[)(.*)(]])

def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): return match.group().replace('[img[,<img"')

What im attempting to achieve is something like this

def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs): return match.group().replace('[img[(.*)]],<img"(.*)/>)

basically grouping a word(and surrounding characters) and modify it in the replace function. My aim is to be able to do this with multiple strings in a row. That is why im using "replace(" as i can put that multiple times in a row.

A single example of i/o is listed below.

[img[imagename.jpg]]

<img"imagename.jpg"/>

1 Answer 1

0

If the fact that text1 and text3 disappeared in your example is just an oversight, I don't think you need multiple capturing groups, just the one for the file name. The brackets and the img string are constant, so you can recreate them.

Your pattern should be something like \[img\[([^]])\]\]. You'd replace it with <img src="\1">.

I say "something like" that pattern because I'm not entirely sure how you need to format it to make sure it gets to the regex engine with all the escape characters intact. In Python you'd want to use a raw string, but I have no idea how you apply a regex using Calibre. Similarly, I don't know if the backreference using \1 will work (but I expect that's the most likely form to work in an unknown regex environment).

4
  • Well, your edits tell me I have no idea what you're asking about. What do the modifications in your two examples have to do with each other?
    – Blckknght
    Mar 15, 2018 at 1:04
  • Thanks for your patience. Apologies for the confusion. Lets just start over with the new code i posted. im struggling with the replace function, and if im able to use grouping within it. Mar 15, 2018 at 2:03
  • Ok, so your not doing a regex replace, but just a regex search, followed by a Python expression on the match object to get the replacement (though your examples have syntax errors and other issues, so I'm still not certain). Try matching on the pattern I suggest in my answer, and replacing with '<img "{}" />'.format(match.group()).
    – Blckknght
    Mar 15, 2018 at 2:09
  • so that did make a bit of progress. here is what happened. im sure i messed it up from this, but i modified your pattern to this as it was not matching anything in the document \[img\[(.*)\]\] Then I used your replace function. The output was this - <img "[img[imagename.jpg]]" /> close... the hope is for this <img "imagename.jpg" /> Mar 15, 2018 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.