Using string replacement ($& and $+) with findReplaceRegex (Confluence CLI tool)


I've been using CLI for a few days now and it has greatly sped up the wiki migration i'm doing. Today i've been doing a lot of findReplace and findReplaceRegex to replace common things throughout very similiar articles, but i ran into a little problem wich i can't seem to figure out.

What i'm trying to achieve is this: i need to find strings that start with '@' and have all capital letters and then, with the match, i want to put <span class='myclass'> </span> around it to get some simple syntax highlighting. I'm not a big expert on regular expressions, but from what i can remember shouldn't it be possible to use string replacement on the right hand part of the findReplaceRegex action call parameter in order to re-use the matched string?

I've found this example at the documentation:

--findReplaceRegex "(a )(test)(.*):\$1\$3#\$2#"

Which seems to be doing something similiar to what i want, using the matched strings again to replace the existing ones. However, i can't get this to work. When i do it like this it just prints \$1\$3#\$2# as string in my article.

Any idea if it's possible to get it working? Would again be a great time saver. This is what i have so far:

--findReplaceRegex "(@)([A-Z):\$&"

Thanks in advance!

1 answer

1 accepted

1 vote
Accepted answer
  1. It helps a lot to get your regex working using this tool:
  2. You need to watch out for special characters (command line and regex) - it can get confusing when you have to escape things and it depends on your OS what needs to be escaped (the example \$ is an escaped $ on unix)
  3. Quick look at your regex shows a few problems
    1. Don't you want [A-Z]* ? You are missing the closing ] and the * (0 or more characters)
    2. You need $1 on the replace
    3. I suspect you need some like:
      (@[A-Z]*):some text\$1some other text

Thanks for your answer, i've checked it out and i managed to get it working! Seems i wasn't far off, i kept using \$1 but it started working when i stopped using the backslash. This works for me:

--findReplaceRegex "(@[A-Z0-9]*):<span class='quaestor_syntax'>$1</span>"

Thanks again for your help!

Good, for Windows, you don't need to escape the $ like for unix.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 24, 2018 in Confluence

Atlassian Research opportunity with Confluence templates

Do you use templates with Confluence? Take part in a remote 1-hr workshop. You'll receive USD $100 for your time!   We're looking for people to participate in a   remote 1-hr workshop...

1,098 views 17 14
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you