I suspect you want to escape the outside brackets because you want them to mean literally brackets in your input and not to denote a capture group in regex. And so you will also have to escape the dot (.) if you need its literal value. (escape: \( \) \.). So something like: if ( $tag =~ m/<endnote id=\(?(\d*)([[:alpha:].]*)\)?>/ ) { (note that dot needs no escaping inside []). But your regex will fail on your 2 last cases: <endnote id=a.1>Text...</endnote>, so why not something like: if ( $tag =~ m/<endnote id=\(?([0-9[:alpha:].]*)\)?>/ )
But if I were you I would split the code in two subs: 1) to clean the input and remove unwanted characters. 2) to parse only properly formatted input. If your input is badly formed XML then perhaps invest doing a proper (1) and then let a proper XML parser do (2). It depends on your use case and how complex it can become in the future. To paraphrase a grand writer: All good data are alike; each bad data is bad in its own way.
bw, bliako