Reputation: 1125
I am trying to match the anchor link that is "triple-escaped" in this string example:
blablabla some text <a href=\\\"#anchor\\\"> some more text
This is my regular expression:
href=(\\\\\\)(\"#.*)(\\\\\\)\"
If I test it on regex101.com it works, but I need to do this filtering in Kotlin, which I thought I could do like this:
fun findEscapedAnchors(text: String): String {
val pattern = "href=(\\\\\\)(\"#.*)(\\\\\\)\""
val regex = pattern.toRegex()
val matches = regex.find(text)
// do something with the matches
}
First of all, if I paste this String into my code (in Android Studio), it gets auto-escaped even more, but it doesn't work. If I edit it to match the above String, it complains that there's an unclosed group. I thought I could maybe put it in triple quotations to not have to escape characters, but that also failed. What am I doing wrong?
Upvotes: 1
Views: 347
Reputation: 1125
I have figured it out myself: A raw string (triple quotations) was indeed the way to go, but Regex
apparently still needs the character escapes in the string. Before, I had them removed, because I thought that's how raw strings work, but I was wrong. So it works now with this:
val regex = """href=(\\\\\\)(\"#.*)(\\\\\\)\"""".toRegex()
Upvotes: 1